@@ -3,7 +3,6 @@ package mongo
33import (
44 "context"
55 "encoding/json"
6- "fmt"
76 "log"
87 "time"
98
@@ -41,12 +40,7 @@ func NewDefaultTokenConfig(strConfig *StoreConfig) *TokenConfig {
4140}
4241
4342// NewTokenStore create a token store instance based on mongodb
44- // func NewTokenStore(cfg *Config, tcfgs ...*TokenConfig) (store *TokenStore) {
4543func NewTokenStore (cfg * Config , scfgs ... * StoreConfig ) (store * TokenStore ) {
46- // clientOptions := options.Client().ApplyURI(cfg.URL).SetWriteConcern(writeconcern.New(writeconcern.WMajority()))
47-
48- fmt .Println ("See url: " , cfg .URL )
49-
5044 clientOptions := options .Client ().ApplyURI (cfg .URL )
5145 ctx := context .TODO ()
5246 ctxPing := context .TODO ()
@@ -91,8 +85,6 @@ func NewTokenStore(cfg *Config, scfgs ...*StoreConfig) (store *TokenStore) {
9185
9286// NewTokenStoreWithSession create a token store instance based on mongodb
9387func NewTokenStoreWithSession (client * mongo.Client , cfg * Config , scfgs ... * StoreConfig ) (store * TokenStore ) {
94- // func NewTokenStoreWithSession(client *mongo.Client, cfg *Config) (store *TokenStore) {
95-
9688 strCfgs := NewDefaultStoreConfig (cfg .DB , cfg .Service , cfg .IsReplicaSet )
9789
9890 ts := & TokenStore {
@@ -205,7 +197,6 @@ func (ts *TokenStore) Create(ctx context.Context, info oauth2.TokenInfo) (err er
205197 }
206198
207199 id := primitive .NewObjectID ().Hex ()
208- // fmt.Println("the id: ", id)
209200
210201 // Create the basicData document
211202 basicData := basicData {
@@ -221,7 +212,7 @@ func (ts *TokenStore) Create(ctx context.Context, info oauth2.TokenInfo) (err er
221212 ExpiredAt : aexp ,
222213 }
223214
224- // if context is defined manually , increase it for the transaction
215+ // if context is defined, increase it for the transaction
225216 ctxTxn , cancel := ts .tcfg .storeConfig .setTransactionCreateContext ()
226217 defer cancel ()
227218 if ctxTxn != nil {
@@ -283,6 +274,12 @@ func (ts *TokenStore) Create(ctx context.Context, info oauth2.TokenInfo) (err er
283274
284275// RemoveByCode use the authorization code to delete the token information
285276func (ts * TokenStore ) RemoveByCode (ctx context.Context , code string ) (err error ) {
277+ ctxReq , cancel := ts .tcfg .storeConfig .setRequestContext ()
278+ defer cancel ()
279+ if ctxReq != nil {
280+ ctx = ctxReq
281+ }
282+
286283 _ , err = ts .c (ts .tcfg .BasicCName ).DeleteOne (ctx , bson.D {{Key : "_id" , Value : code }})
287284 if err != nil {
288285 log .Println ("Error RemoveByCode: " , err )
@@ -292,6 +289,12 @@ func (ts *TokenStore) RemoveByCode(ctx context.Context, code string) (err error)
292289
293290// RemoveByAccess use the access token to delete the token information
294291func (ts * TokenStore ) RemoveByAccess (ctx context.Context , access string ) (err error ) {
292+ ctxReq , cancel := ts .tcfg .storeConfig .setRequestContext ()
293+ defer cancel ()
294+ if ctxReq != nil {
295+ ctx = ctxReq
296+ }
297+
295298 _ , err = ts .c (ts .tcfg .AccessCName ).DeleteOne (ctx , bson.D {{Key : "_id" , Value : access }})
296299 if err != nil {
297300 log .Println ("Error RemoveByAccess: " , err )
@@ -301,6 +304,12 @@ func (ts *TokenStore) RemoveByAccess(ctx context.Context, access string) (err er
301304
302305// RemoveByRefresh use the refresh token to delete the token information
303306func (ts * TokenStore ) RemoveByRefresh (ctx context.Context , refresh string ) (err error ) {
307+ ctxReq , cancel := ts .tcfg .storeConfig .setRequestContext ()
308+ defer cancel ()
309+ if ctxReq != nil {
310+ ctx = ctxReq
311+ }
312+
304313 _ , err = ts .c (ts .tcfg .RefreshCName ).DeleteOne (ctx , bson.D {{Key : "_id" , Value : refresh }})
305314 if err != nil {
306315 log .Println ("Error RemoveByRefresh: " , err )
@@ -309,9 +318,15 @@ func (ts *TokenStore) RemoveByRefresh(ctx context.Context, refresh string) (err
309318}
310319
311320func (ts * TokenStore ) getData (basicID string ) (ti oauth2.TokenInfo , err error ) {
321+ ctx := context .Background ()
322+ ctxReq , cancel := ts .tcfg .storeConfig .setRequestContext ()
323+ defer cancel ()
324+ if ctxReq != nil {
325+ ctx = ctxReq
326+ }
312327
313328 var bd basicData
314- err = ts .c (ts .tcfg .BasicCName ).FindOne (context . TODO () , bson.D {{Key : "_id" , Value : basicID }}).Decode (& bd )
329+ err = ts .c (ts .tcfg .BasicCName ).FindOne (ctx , bson.D {{Key : "_id" , Value : basicID }}).Decode (& bd )
315330 if err != nil {
316331 if err == mongo .ErrNoDocuments {
317332 return nil , nil
@@ -329,8 +344,15 @@ func (ts *TokenStore) getData(basicID string) (ti oauth2.TokenInfo, err error) {
329344}
330345
331346func (ts * TokenStore ) getBasicID (cname , token string ) (basicID string , err error ) {
347+ ctx := context .Background ()
348+ ctxReq , cancel := ts .tcfg .storeConfig .setRequestContext ()
349+ defer cancel ()
350+ if ctxReq != nil {
351+ ctx = ctxReq
352+ }
353+
332354 var td tokenData
333- err = ts .c (cname ).FindOne (context . TODO () , bson.D {{Key : "_id" , Value : token }}).Decode (& td )
355+ err = ts .c (cname ).FindOne (ctx , bson.D {{Key : "_id" , Value : token }}).Decode (& td )
334356 if err != nil {
335357 if err == mongo .ErrNoDocuments {
336358 return
0 commit comments