Skip to content

Commit

Permalink
Merge pull request #14 from FrosTiK-SD/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Shubhrajyoti-Dey-FrosTiK authored Jan 19, 2024
2 parents a335ceb + 3dd4305 commit 9c7b39c
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 823 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.20
FROM golang:1.21.4

ARG ATLAS_URI
ARG FIREBASE_PROJECT_ID
Expand All @@ -16,7 +16,7 @@ COPY go.sum .
RUN go mod download

COPY . .
RUN go build -o authv2
RUN go build -tags=jsoniter -o authv2

EXPOSE 8080

Expand Down
6 changes: 3 additions & 3 deletions Dockerfile.production
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.20 as builder
FROM golang:1.21.4 as builder

ARG ATLAS_URI
ARG FIREBASE_PROJECT_ID
Expand All @@ -13,10 +13,10 @@ WORKDIR "$APP_HOME"
COPY . .

RUN go mod download
RUN go build -o authv2
RUN go build -tags=jsoniter -o authv2

# copy build to a clean image
FROM golang:1.20
FROM golang:1.21.4

ARG ATLAS_URI
ARG FIREBASE_PROJECT_ID
Expand Down
34 changes: 14 additions & 20 deletions controller/student.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package controller

import (
"encoding/json"
"fmt"
"sort"
"strings"

"frostik.com/auth/constants"
"frostik.com/auth/mapper"
"frostik.com/auth/model"
"frostik.com/auth/util"
db "github.com/FrosTiK-SD/mongik/db"
models "github.com/FrosTiK-SD/mongik/models"
jsoniter "github.com/json-iterator/go"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)

var json = jsoniter.ConfigCompatibleWithStandardLibrary

func getAliasEmailList(email string) []string {
var aliasEmailList []string
aliasEmailList = append(aliasEmailList, email)
Expand All @@ -26,7 +26,6 @@ func getAliasEmailList(email string) []string {
}

func GetUserByEmail(mongikClient *models.Mongik, email *string, role *string, noCache bool) (*model.StudentPopulated, *string) {
var student model.Student
var studentPopulated model.StudentPopulated

// Check if copy is there in the cache
Expand All @@ -43,24 +42,19 @@ func GetUserByEmail(mongikClient *models.Mongik, email *string, role *string, no

// Query to DB
fmt.Println("Queriying the DB for User Details")
db.FindOne[model.Student](mongikClient, constants.DB, constants.COLLECTION_STUDENT, bson.M{
"email": bson.M{"$in": emailList},
}, &student, noCache)
studentPopulated = mapper.TransformStudentToStudentPopulated(student)

var groupIds = []primitive.ObjectID{}
var groupDetails = []model.Group{}
for _, id := range student.Groups {
groupIds = append(groupIds, id)
}

groupDetails, _ = db.Find[model.Group](mongikClient, constants.DB, constants.COLLECTION_GROUP, bson.M{
"_id": bson.M{"$in": groupIds},
}, noCache)
studentPopulated.Groups = groupDetails
studentPopulated, _ = db.AggregateOne[model.StudentPopulated](mongikClient, constants.DB, constants.COLLECTION_STUDENT, []bson.M{{
"$match": bson.M{"email": bson.M{"$in": emailList}},
}, {
"$lookup": bson.M{
"from": constants.COLLECTION_GROUP,
"localField": "groups",
"foreignField": "_id",
"as": "groups",
},
}}, noCache)

// Now check if it is actually a student by the ROLES
if !util.CheckRoleExists(&groupDetails, *role) {
if !util.CheckRoleExists(&studentPopulated.Groups, *role) {
return nil, &constants.ERROR_NOT_A_STUDENT
}

Expand Down
15 changes: 8 additions & 7 deletions controller/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,34 +60,35 @@ func GetJWKs(cacheClient *bigcache.BigCache, noCache bool) (*jwk.Set, *string) {
return &jwkSet, nil
}

func VerifyToken(cacheClient *bigcache.BigCache, idToken string, defaultJwkSet *jwk.Set, noCache bool) (*string, *string) {
func VerifyToken(cacheClient *bigcache.BigCache, idToken string, defaultJwkSet *jwk.Set, noCache bool) (*string, *time.Time, *string) {
jwkSet := defaultJwkSet
if !noCache {
newJwkSet, jwkParsingError := GetJWKs(cacheClient, noCache)
if jwkParsingError != nil {
return nil, jwkParsingError
return nil, nil, jwkParsingError
}
jwkSet = newJwkSet
}

// Verify the token
rawJWT, err := jwt.Parse([]byte(idToken), jwt.WithKeySet(*jwkSet))
if err != nil {
return nil, &constants.ERROR_TOKEN_SIGNATURE_INVALID
return nil, nil, &constants.ERROR_TOKEN_SIGNATURE_INVALID
}
exp := rawJWT.Expiration()

// Validations
if time.Now().Sub(rawJWT.IssuedAt()) < 0 || time.Now().Sub(rawJWT.Expiration()) > 0 || rawJWT.Subject() == "" || rawJWT.Issuer() != fmt.Sprintf("https://securetoken.google.com/%s", os.Getenv(constants.FIREBASE_PROJECT_ID)) || !util.ArrayContains(rawJWT.Audience(), os.Getenv(constants.FIREBASE_PROJECT_ID)) {
return nil, &constants.ERROR_INVALID_TOKEN
if time.Now().Sub(rawJWT.IssuedAt()) < 0 || time.Now().Sub(exp) > 0 || rawJWT.Subject() == "" || rawJWT.Issuer() != fmt.Sprintf("https://securetoken.google.com/%s", os.Getenv(constants.FIREBASE_PROJECT_ID)) || !util.ArrayContains(rawJWT.Audience(), os.Getenv(constants.FIREBASE_PROJECT_ID)) {
return nil, &exp, &constants.ERROR_INVALID_TOKEN
}

// Get the email
email, found := rawJWT.Get("email")
if found == false {
return nil, &constants.ERROR_GETTING_EMAIL
return nil, &exp, &constants.ERROR_GETTING_EMAIL
}

emailString := fmt.Sprintf("%v", email)

return &emailString, nil
return &emailString, &exp, nil
}
32 changes: 5 additions & 27 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,45 +1,34 @@
module frostik.com/auth

go 1.21
go 1.21.1

require (
github.com/FrosTiK-SD/mongik v0.1.5
github.com/allegro/bigcache/v3 v3.1.0
github.com/gin-gonic/gin v1.9.0
github.com/joho/godotenv v1.5.1
google.golang.org/api v0.96.0
github.com/lestrrat-go/jwx/v2 v2.0.16
go.mongodb.org/mongo-driver v1.13.0
)

require (
github.com/FrosTiK-SD/mongik v0.1.3 // indirect
github.com/allegro/bigcache/v3 v3.1.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/golang/snappy v0.0.3 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/lestrrat-go/backoff/v2 v2.0.8 // indirect
github.com/lestrrat-go/blackmagic v1.0.2 // indirect
github.com/lestrrat-go/httpcc v1.0.1 // indirect
github.com/lestrrat-go/httprc v1.0.4 // indirect
github.com/lestrrat-go/iter v1.0.2 // indirect
github.com/lestrrat-go/jwx v1.2.26 // indirect
github.com/lestrrat-go/jwx/v2 v2.0.16 // indirect
github.com/lestrrat-go/option v1.0.1 // indirect
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/segmentio/asm v1.2.0 // indirect
github.com/vmihailenco/go-tinylfu v0.2.2 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.4 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
go.mongodb.org/mongo-driver v1.13.0 // indirect
golang.org/x/sync v0.1.0 // indirect
)

require (
cloud.google.com/go v0.102.1 // indirect
github.com/bytedance/sonic v1.8.2 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/gin-contrib/cors v1.4.0
Expand All @@ -48,12 +37,7 @@ require (
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.11.2 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.1.0 // indirect
github.com/googleapis/gax-go/v2 v2.4.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/leodido/go-urn v1.2.2 // indirect
Expand All @@ -63,17 +47,11 @@ require (
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.10 // indirect
go.opencensus.io v0.23.0 // indirect
golang.org/x/arch v0.2.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/oauth2 v0.0.0-20220909003341-f21342109be1 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/xerrors v0.0.0-20220609144429-65e65417b02f // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20220810155839-1856144b1d9c // indirect
google.golang.org/grpc v1.48.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 9c7b39c

Please sign in to comment.