@@ -2,6 +2,9 @@ package accounts
22
33import (
44 "context"
5+ "encoding/base64"
6+ "encoding/json"
7+ "strings"
58 "time"
69
710 "go.mongodb.org/mongo-driver/bson/primitive"
@@ -14,6 +17,11 @@ import (
1417const projectsCollectionName = "projects"
1518const contextTimeout = 5 * time .Second
1619
20+ type acountToken struct {
21+ IntegrationId string `json:"integrationId"`
22+ Secret string `json:"secret"`
23+ }
24+
1725type accountProject struct {
1826 ProjectID primitive.ObjectID `bson:"_id"`
1927 Token string `bson:"token"`
@@ -39,11 +47,33 @@ func (client *AccountsMongoDBClient) UpdateTokenCache() error {
3947
4048 client .ValidTokens = make (map [string ]string )
4149 for _ , project := range projects {
42- client .ValidTokens [project .Token ] = project .ProjectID .Hex ()
50+ integrationSecret , err := DecodeToken (project .Token )
51+ if err == nil {
52+ client .ValidTokens [integrationSecret ] = project .ProjectID .Hex ()
53+ } else {
54+ log .Errorf ("Integration token %s is invalid: %s" , project .Token , err )
55+ }
4356 }
4457
4558 log .Debugf ("Cache for MongoDB tokens successfully updates with %d tokens" , len (client .ValidTokens ))
4659 log .Tracef ("Current token cache state: %s" , client .ValidTokens )
4760
4861 return nil
4962}
63+
64+ // decodeToken decodes token from base64 to integrationId + secret
65+ func DecodeToken (token string ) (string , error ) {
66+ decoded , err := base64 .StdEncoding .DecodeString (token )
67+ if err != nil {
68+ return "" , err
69+ }
70+ var data acountToken
71+ err = json .Unmarshal (decoded , & data )
72+ if err != nil {
73+ return "" , err
74+ }
75+
76+ integrationId := strings .ReplaceAll (data .IntegrationId , "-" , "" )
77+ secret := strings .ReplaceAll (data .Secret , "-" , "" )
78+ return integrationId + secret , nil
79+ }
0 commit comments