|
| 1 | +package mongo |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/globalsign/mgo" |
| 5 | + "gopkg.in/oauth2.v3" |
| 6 | + "gopkg.in/oauth2.v3/models" |
| 7 | +) |
| 8 | + |
| 9 | +// ClientConfig client configuration parameters |
| 10 | +type ClientConfig struct { |
| 11 | + // store clients data collection name(The default is oauth2_clients) |
| 12 | + ClientsCName string |
| 13 | +} |
| 14 | + |
| 15 | +// NewDefaultClientConfig create a default client configuration |
| 16 | +func NewDefaultClientConfig() *ClientConfig { |
| 17 | + return &ClientConfig{ |
| 18 | + ClientsCName: "oauth2_clients", |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +// NewClientStore create a client store instance based on mongodb |
| 23 | +func NewClientStore(cfg *Config, ccfgs ...*ClientConfig) *ClientStore { |
| 24 | + session, err := mgo.Dial(cfg.URL) |
| 25 | + if err != nil { |
| 26 | + panic(err) |
| 27 | + } |
| 28 | + |
| 29 | + return NewClientStoreWithSession(session, cfg.DB, ccfgs...) |
| 30 | +} |
| 31 | + |
| 32 | +// NewClientStoreWithSession create a client store instance based on mongodb |
| 33 | +func NewClientStoreWithSession(session *mgo.Session, dbName string, ccfgs ...*ClientConfig) *ClientStore { |
| 34 | + cs := &ClientStore{ |
| 35 | + dbName: dbName, |
| 36 | + session: session, |
| 37 | + ccfg: NewDefaultClientConfig(), |
| 38 | + } |
| 39 | + if len(ccfgs) > 0 { |
| 40 | + cs.ccfg = ccfgs[0] |
| 41 | + } |
| 42 | + |
| 43 | + return cs |
| 44 | +} |
| 45 | + |
| 46 | +// ClientStore MongoDB storage for OAuth 2.0 |
| 47 | +type ClientStore struct { |
| 48 | + ccfg *ClientConfig |
| 49 | + dbName string |
| 50 | + session *mgo.Session |
| 51 | +} |
| 52 | + |
| 53 | +// Close close the mongo session |
| 54 | +func (cs *ClientStore) Close() { |
| 55 | + cs.session.Close() |
| 56 | +} |
| 57 | + |
| 58 | +func (cs *ClientStore) c(name string) *mgo.Collection { |
| 59 | + return cs.session.DB(cs.dbName).C(name) |
| 60 | +} |
| 61 | + |
| 62 | +func (cs *ClientStore) cHandler(name string, handler func(c *mgo.Collection)) { |
| 63 | + session := cs.session.Clone() |
| 64 | + defer session.Close() |
| 65 | + handler(session.DB(cs.dbName).C(name)) |
| 66 | + return |
| 67 | +} |
| 68 | + |
| 69 | +// Set set client information |
| 70 | +func (cs *ClientStore) Set(info oauth2.ClientInfo) (err error) { |
| 71 | + cs.cHandler(cs.ccfg.ClientsCName, func(c *mgo.Collection) { |
| 72 | + entity := &client{ |
| 73 | + ID: info.GetID(), |
| 74 | + Secret: info.GetSecret(), |
| 75 | + Domain: info.GetDomain(), |
| 76 | + UserID: info.GetUserID(), |
| 77 | + } |
| 78 | + |
| 79 | + if cerr := c.Insert(entity); cerr != nil { |
| 80 | + err = cerr |
| 81 | + return |
| 82 | + } |
| 83 | + }) |
| 84 | + |
| 85 | + return |
| 86 | +} |
| 87 | + |
| 88 | +// GetByID according to the ID for the client information |
| 89 | +func (cs *ClientStore) GetByID(id string) (info oauth2.ClientInfo, err error) { |
| 90 | + cs.cHandler(cs.ccfg.ClientsCName, func(c *mgo.Collection) { |
| 91 | + entity := new(client) |
| 92 | + |
| 93 | + if cerr := c.FindId(id).One(entity); cerr != nil { |
| 94 | + err = cerr |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + info = &models.Client{ |
| 99 | + ID: entity.ID, |
| 100 | + Secret: entity.Secret, |
| 101 | + Domain: entity.Domain, |
| 102 | + UserID: entity.UserID, |
| 103 | + } |
| 104 | + }) |
| 105 | + |
| 106 | + return |
| 107 | +} |
| 108 | + |
| 109 | +// RemoveByID use the client id to delete the client information |
| 110 | +func (cs *ClientStore) RemoveByID(id string) (err error) { |
| 111 | + cs.cHandler(cs.ccfg.ClientsCName, func(c *mgo.Collection) { |
| 112 | + if cerr := c.RemoveId(id); cerr != nil { |
| 113 | + err = cerr |
| 114 | + return |
| 115 | + } |
| 116 | + }) |
| 117 | + |
| 118 | + return |
| 119 | +} |
| 120 | + |
| 121 | +type client struct { |
| 122 | + ID string `bson:"_id"` |
| 123 | + Secret string `bson:"secret"` |
| 124 | + Domain string `bson:"domain"` |
| 125 | + UserID string `bson:"userid"` |
| 126 | +} |
0 commit comments