@@ -4,13 +4,27 @@ import (
44"encoding/json"
55"time"
66
7- "gopkg.in/ mgo.v2 "
8- "gopkg.in/ mgo.v2 /bson"
9- "gopkg.in/ mgo.v2 /txn"
7+ "github.com/globalsign/ mgo"
8+ "github.com/globalsign/ mgo/bson"
9+ "github.com/globalsign/ mgo/txn"
1010"gopkg.in/oauth2.v3"
1111"gopkg.in/oauth2.v3/models"
1212)
1313
14+ // Config mongodb configuration parameters
15+ type Config struct {
16+ URL string
17+ DB string
18+ }
19+
20+ // NewConfig create mongodb configuration
21+ func NewConfig (url , db string ) * Config {
22+ return & Config {
23+ URL : url ,
24+ DB : db ,
25+ }
26+ }
27+
1428// TokenConfig token configuration parameters
1529type TokenConfig struct {
1630// store txn collection name(The default is oauth2)
@@ -34,59 +48,65 @@ func NewDefaultTokenConfig() *TokenConfig {
3448}
3549
3650// NewTokenStore create a token store instance based on mongodb
37- func NewTokenStore (cfg * Config , tcfgs ... * TokenConfig ) (store oauth2.TokenStore , err error ) {
51+ func NewTokenStore (cfg * Config , tcfgs ... * TokenConfig ) (store * TokenStore ) {
52+ session , err := mgo .Dial (cfg .URL )
53+ if err != nil {
54+ panic (err )
55+ }
56+
57+ return NewTokenStoreWithSession (session , cfg .DB , tcfgs ... )
58+ }
59+
60+ // NewTokenStoreWithSession create a token store instance based on mongodb
61+ func NewTokenStoreWithSession (session * mgo.Session , dbName string , tcfgs ... * TokenConfig ) (store * TokenStore ) {
3862ts := & TokenStore {
39- mcfg : cfg ,
40- tcfg : NewDefaultTokenConfig (),
63+ dbName : dbName ,
64+ session : session ,
65+ tcfg : NewDefaultTokenConfig (),
4166}
4267if len (tcfgs ) > 0 {
4368ts .tcfg = tcfgs [0 ]
4469}
45- session , err := mgo .Dial (ts .mcfg .URL )
46- if err != nil {
47- return
48- }
49- ts .session = session
50- err = ts .c (ts .tcfg .BasicCName ).EnsureIndex (mgo.Index {
70+
71+ ts .c (ts .tcfg .BasicCName ).EnsureIndex (mgo.Index {
5172Key : []string {"ExpiredAt" },
5273ExpireAfter : time .Second * 1 ,
5374})
54- if err != nil {
55- return
56- }
57- err = ts .c (ts .tcfg .AccessCName ).EnsureIndex (mgo.Index {
75+
76+ ts .c (ts .tcfg .AccessCName ).EnsureIndex (mgo.Index {
5877Key : []string {"ExpiredAt" },
5978ExpireAfter : time .Second * 1 ,
6079})
61- if err != nil {
62- return
63- }
64- err = ts .c (ts .tcfg .RefreshCName ).EnsureIndex (mgo.Index {
80+
81+ ts .c (ts .tcfg .RefreshCName ).EnsureIndex (mgo.Index {
6582Key : []string {"ExpiredAt" },
6683ExpireAfter : time .Second * 1 ,
6784})
68- if err != nil {
69- return
70- }
85+
7186store = ts
7287return
7388}
7489
7590// TokenStore MongoDB storage for OAuth 2.0
7691type TokenStore struct {
7792tcfg * TokenConfig
78- mcfg * Config
93+ dbName string
7994session * mgo.Session
8095}
8196
97+ // Close close the mongo session
98+ func (ts * TokenStore ) Close () {
99+ ts .session .Close ()
100+ }
101+
82102func (ts * TokenStore ) c (name string ) * mgo.Collection {
83- return ts .session .DB (ts .mcfg . DB ).C (name )
103+ return ts .session .DB (ts .dbName ).C (name )
84104}
85105
86106func (ts * TokenStore ) cHandler (name string , handler func (c * mgo.Collection )) {
87107session := ts .session .Clone ()
88108defer session .Close ()
89- handler (session .DB (ts .mcfg . DB ).C (name ))
109+ handler (session .DB (ts .dbName ).C (name ))
90110return
91111}
92112
0 commit comments