@@ -4,13 +4,27 @@ import (
4
4
"encoding/json"
5
5
"time"
6
6
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"
10
10
"gopkg.in/oauth2.v3"
11
11
"gopkg.in/oauth2.v3/models"
12
12
)
13
13
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
+
14
28
// TokenConfig token configuration parameters
15
29
type TokenConfig struct {
16
30
// store txn collection name(The default is oauth2)
@@ -34,59 +48,65 @@ func NewDefaultTokenConfig() *TokenConfig {
34
48
}
35
49
36
50
// 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 ) {
38
62
ts := & TokenStore {
39
- mcfg : cfg ,
40
- tcfg : NewDefaultTokenConfig (),
63
+ dbName : dbName ,
64
+ session : session ,
65
+ tcfg : NewDefaultTokenConfig (),
41
66
}
42
67
if len (tcfgs ) > 0 {
43
68
ts .tcfg = tcfgs [0 ]
44
69
}
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 {
51
72
Key : []string {"ExpiredAt" },
52
73
ExpireAfter : time .Second * 1 ,
53
74
})
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 {
58
77
Key : []string {"ExpiredAt" },
59
78
ExpireAfter : time .Second * 1 ,
60
79
})
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 {
65
82
Key : []string {"ExpiredAt" },
66
83
ExpireAfter : time .Second * 1 ,
67
84
})
68
- if err != nil {
69
- return
70
- }
85
+
71
86
store = ts
72
87
return
73
88
}
74
89
75
90
// TokenStore MongoDB storage for OAuth 2.0
76
91
type TokenStore struct {
77
92
tcfg * TokenConfig
78
- mcfg * Config
93
+ dbName string
79
94
session * mgo.Session
80
95
}
81
96
97
+ // Close close the mongo session
98
+ func (ts * TokenStore ) Close () {
99
+ ts .session .Close ()
100
+ }
101
+
82
102
func (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 )
84
104
}
85
105
86
106
func (ts * TokenStore ) cHandler (name string , handler func (c * mgo.Collection )) {
87
107
session := ts .session .Clone ()
88
108
defer session .Close ()
89
- handler (session .DB (ts .mcfg . DB ).C (name ))
109
+ handler (session .DB (ts .dbName ).C (name ))
90
110
return
91
111
}
92
112
0 commit comments