Skip to content

Commit

Permalink
AuthV2 middleware support for GIN
Browse files Browse the repository at this point in the history
  • Loading branch information
Shubhrajyoti-Dey-FrosTiK committed Jan 28, 2024
1 parent 2a39d01 commit f7289dd
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 38 deletions.
8 changes: 3 additions & 5 deletions controller/student.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package controller

import (
"fmt"
"sort"
"strings"

"frostik.com/auth/constants"
"frostik.com/auth/model"
"frostik.com/auth/util"
"github.com/FrosTiK-SD/authV2/constants"
"github.com/FrosTiK-SD/authV2/model"
"github.com/FrosTiK-SD/authV2/util"
db "github.com/FrosTiK-SD/mongik/db"
models "github.com/FrosTiK-SD/mongik/models"
jsoniter "github.com/json-iterator/go"
Expand All @@ -32,7 +31,6 @@ func GetUserByEmail(mongikClient *models.Mongik, email *string, role *string, no
emailList := getAliasEmailList(*email)

// Query to DB
fmt.Println("Queriying the DB for User Details")
studentPopulated, _ = db.AggregateOne[model.StudentPopulated](mongikClient, constants.DB, constants.COLLECTION_STUDENT, []bson.M{{
"$match": bson.M{"email": bson.M{"$in": emailList}},
}, {
Expand Down
4 changes: 2 additions & 2 deletions controller/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"os"
"time"

"frostik.com/auth/constants"
"frostik.com/auth/util"
"github.com/FrosTiK-SD/authV2/constants"
"github.com/FrosTiK-SD/authV2/util"
"github.com/allegro/bigcache/v3"
"github.com/lestrrat-go/jwx/v2/jwk"
"github.com/lestrrat-go/jwx/v2/jwt"
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module frostik.com/auth
module github.com/FrosTiK-SD/authV2

go 1.21.1

require (
github.com/FrosTiK-SD/mongik v0.1.17
github.com/FrosTiK-SD/mongik v0.1.18
github.com/allegro/bigcache/v3 v3.1.0
github.com/gin-gonic/gin v1.9.0
github.com/lestrrat-go/jwx/v2 v2.0.16
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/FrosTiK-SD/mongik v0.1.5 h1:CkredRLbvy6WmV81Ynl5INq8DPvR73gKYbnqoH/WE
github.com/FrosTiK-SD/mongik v0.1.5/go.mod h1:EAFjp0U+R9SQcEe2tCa7In2lc58bhmO+0a08Ss3OaN4=
github.com/FrosTiK-SD/mongik v0.1.17 h1:hCcohfz7rRG5om6+5K84RZ2Nlz4UpZg6DUY+zWmyKCc=
github.com/FrosTiK-SD/mongik v0.1.17/go.mod h1:AjFFmUGUAix1sf24uxh0Wxq5fNIpykx4EEd44AMYhfw=
github.com/FrosTiK-SD/mongik v0.1.18 h1:kDhGDEhgDyfEG4lEkVRfe2mVrtDB9QDsY9g2HOASjCw=
github.com/FrosTiK-SD/mongik v0.1.18/go.mod h1:AjFFmUGUAix1sf24uxh0Wxq5fNIpykx4EEd44AMYhfw=
github.com/allegro/bigcache/v3 v3.1.0 h1:H2Vp8VOvxcrB91o86fUSVJFqeuz8kpyyB02eH3bSzwk=
github.com/allegro/bigcache/v3 v3.1.0/go.mod h1:aPyh7jEvrog9zAwx5N7+JUQX5dZTSGpxF1LAR4dr35I=
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
Expand Down
41 changes: 41 additions & 0 deletions handler/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package handler

import (
"github.com/FrosTiK-SD/authV2/controller"
"github.com/FrosTiK-SD/authV2/model"
mongik "github.com/FrosTiK-SD/mongik/models"
"github.com/lestrrat-go/jwx/v2/jwk"
)

type Mode string

const (
HANDLER Mode = "HANDLER"
MIDDLEWARE Mode = "MIDDLEWARE"
)

type Session struct {
Student *model.StudentPopulated
}

type Handler struct {
MongikClient *mongik.Mongik
JwkSet *jwk.Set
Session *Session
Config Config
}

type Config struct {
Mode Mode
}

func NewAuthClient(mongik *mongik.Mongik) *Handler {
defaultJwkSet, _ := controller.GetJWKs(mongik.CacheClient, false)
return &Handler{
MongikClient: mongik,
JwkSet: defaultJwkSet,
Config: Config{
Mode: MIDDLEWARE,
},
}
}
27 changes: 27 additions & 0 deletions handler/middleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package handler

import (
"github.com/gin-gonic/gin"
)

// For Gin based middlewares
func (h *Handler) GinVerifyStudent(ctx *gin.Context) {

// Create a new session
currentHandler := Handler{
MongikClient: h.MongikClient,
JwkSet: h.JwkSet,
Session: &Session{},
Config: Config{
Mode: MIDDLEWARE,
},
}

currentHandler.HandlerVerifyStudentIdToken(ctx)
student := currentHandler.Session.Student

if student != nil {
ctx.Set("student", student)
ctx.Next()
}
}
31 changes: 14 additions & 17 deletions handler/verify.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
package handler

import (
"frostik.com/auth/constants"
"frostik.com/auth/controller"
models "github.com/FrosTiK-SD/mongik/models"
"github.com/allegro/bigcache/v3"
"github.com/FrosTiK-SD/authV2/constants"
"github.com/FrosTiK-SD/authV2/controller"

"github.com/gin-gonic/gin"
"github.com/lestrrat-go/jwx/v2/jwk"
)

type Handler struct {
MongikClient *models.Mongik
JwkSet *jwk.Set
BigCache *bigcache.BigCache
}

func (h *Handler) HandlerVerifyStudentIdToken(ctx *gin.Context) {
idToken := ctx.GetHeader("token")
noCache := false
if ctx.GetHeader("cache-control") == constants.NO_CACHE {
noCache = true
}

email, exp, err := controller.VerifyToken(h.BigCache, idToken, h.JwkSet, noCache)
email, exp, err := controller.VerifyToken(h.MongikClient.CacheClient, idToken, h.JwkSet, noCache)

if err != nil {
ctx.JSON(200, gin.H{
Expand All @@ -32,12 +24,17 @@ func (h *Handler) HandlerVerifyStudentIdToken(ctx *gin.Context) {
})
} else {
student, err := controller.GetUserByEmail(h.MongikClient, email, &constants.ROLE_STUDENT, noCache)
ctx.JSON(200, gin.H{
"data": student,
"error": err,
"expire": exp,
})
if h.Config.Mode == MIDDLEWARE {
h.Session.Student = student
} else {
ctx.JSON(200, gin.H{
"data": student,
"error": err,
"expire": exp,
})
}
}

}

func (h *Handler) InvalidateCache(ctx *gin.Context) {
Expand Down
19 changes: 9 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package main

import (
"context"
"fmt"
"os"

"frostik.com/auth/constants"
"frostik.com/auth/controller"
"frostik.com/auth/handler"
"frostik.com/auth/util"
"github.com/FrosTiK-SD/authV2/constants"
"github.com/FrosTiK-SD/authV2/controller"
"github.com/FrosTiK-SD/authV2/handler"
"github.com/FrosTiK-SD/authV2/util"
"github.com/FrosTiK-SD/mongik"
mongikConstants "github.com/FrosTiK-SD/mongik/constants"
mongikModels "github.com/FrosTiK-SD/mongik/models"
"github.com/allegro/bigcache/v3"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
Expand All @@ -30,10 +28,8 @@ func main() {
},
})

cacheClient, _ := bigcache.New(context.Background(), bigcache.DefaultConfig(constants.CACHING_DURATION))

// Initialie default JWKs
defaultJwkSet, jwkSetRetrieveError := controller.GetJWKs(cacheClient, true)
defaultJwkSet, jwkSetRetrieveError := controller.GetJWKs(mongikClient.CacheClient, true)
if jwkSetRetrieveError != nil {
fmt.Println("Error retrieving JWKs")
}
Expand All @@ -42,8 +38,11 @@ func main() {

handler := &handler.Handler{
MongikClient: mongikClient,
BigCache: cacheClient,
JwkSet: defaultJwkSet,
Config: handler.Config{
Mode: handler.HANDLER,
},
Session: &handler.Session{},
}

r.GET("/api/token/student/verify", handler.HandlerVerifyStudentIdToken)
Expand Down
2 changes: 1 addition & 1 deletion mapper/student.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package mapper

import (
"frostik.com/auth/model"
"github.com/FrosTiK-SD/authV2/model"
)

func TransformStudentToStudentPopulated(student model.Student) model.StudentPopulated {
Expand Down
2 changes: 1 addition & 1 deletion util/role.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package util

import "frostik.com/auth/model"
import "github.com/FrosTiK-SD/authV2/model"

func CheckRoleExists(groups *[]model.Group, role string) bool {
checkRoleStatus := false
Expand Down

0 comments on commit f7289dd

Please sign in to comment.