Skip to content

Commit 3d506d2

Browse files
committed
use globalsign/mgo
1 parent 80af0e5 commit 3d506d2

File tree

5 files changed

+84
-61
lines changed

5 files changed

+84
-61
lines changed

.travis.yml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
language: go
2+
sudo: false
3+
go_import_path: gopkg.in/go-oauth2/mongo.v3
4+
go:
5+
- 1.7
6+
services:
7+
- mongodb
8+
before_install:
9+
- go get -t -v ./...
10+
11+
script:
12+
- go test -race -coverprofile=coverage.txt -covermode=atomic
13+
14+
after_success:
15+
- bash <(curl -s https://codecov.io/bash)

README.md

+16-15
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
# MongoDB Storage for OAuth 2.0
1+
# Mongo Storage for [OAuth 2.0](https://github.com/go-oauth2/oauth2)
22

3-
> Based on the mongodb token storage
4-
5-
[![License][License-Image]][License-Url]
6-
[![ReportCard][ReportCard-Image]][ReportCard-Url]
7-
[![GoDoc][GoDoc-Image]][GoDoc-Url]
3+
[![Build][Build-Status-Image]][Build-Status-Url] [![Codecov][codecov-image]][codecov-url] [![ReportCard][reportcard-image]][reportcard-url] [![GoDoc][godoc-image]][godoc-url] [![License][license-image]][license-url]
84

95
## Install
106

117
``` bash
12-
$ go get -u -v gopkg.in/go-oauth2/mongo.v1
8+
$ go get -u -v gopkg.in/go-oauth2/mongo.v3
139
```
1410

1511
## Usage
@@ -18,14 +14,15 @@ $ go get -u -v gopkg.in/go-oauth2/mongo.v1
1814
package main
1915

2016
import (
21-
"gopkg.in/go-oauth2/mongo.v1"
17+
"gopkg.in/go-oauth2/mongo.v3"
2218
"gopkg.in/oauth2.v3/manage"
2319
)
2420

2521
func main() {
2622
manager := manage.NewDefaultManager()
23+
2724
// use mongodb token store
28-
manager.MustTokenStorage(
25+
manager.MapTokenStorage(
2926
mongo.NewTokenStore(mongo.NewConfig(
3027
"mongodb://127.0.0.1:27017",
3128
"oauth2",
@@ -41,9 +38,13 @@ func main() {
4138
Copyright (c) 2016 Lyric
4239
```
4340

44-
[License-Url]: http://opensource.org/licenses/MIT
45-
[License-Image]: https://img.shields.io/npm/l/express.svg
46-
[ReportCard-Url]: https://goreportcard.com/report/github.com/go-oauth2/mongo
47-
[ReportCard-Image]: https://goreportcard.com/badge/github.com/go-oauth2/mongo
48-
[GoDoc-Url]: https://godoc.org/github.com/go-oauth2/mongo
49-
[GoDoc-Image]: https://godoc.org/github.com/go-oauth2/mongo?status.svg
41+
[Build-Status-Url]: https://travis-ci.org/go-oauth2/mongo
42+
[Build-Status-Image]: https://travis-ci.org/go-oauth2/mongo.svg?branch=master
43+
[codecov-url]: https://codecov.io/gh/go-oauth2/mongo
44+
[codecov-image]: https://codecov.io/gh/go-oauth2/mongo/branch/master/graph/badge.svg
45+
[reportcard-url]: https://goreportcard.com/report/gopkg.in/go-oauth2/mongo.v3
46+
[reportcard-image]: https://goreportcard.com/badge/gopkg.in/go-oauth2/mongo.v3
47+
[godoc-url]: https://godoc.org/gopkg.in/go-oauth2/mongo.v3
48+
[godoc-image]: https://godoc.org/gopkg.in/go-oauth2/mongo.v3?status.svg
49+
[license-url]: http://opensource.org/licenses/MIT
50+
[license-image]: https://img.shields.io/npm/l/express.svg

config.go

-15
This file was deleted.

token.go renamed to mongo.go

+46-26
Original file line numberDiff line numberDiff line change
@@ -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
1529
type 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) {
3862
ts := &TokenStore{
39-
mcfg: cfg,
40-
tcfg: NewDefaultTokenConfig(),
63+
dbName: dbName,
64+
session: session,
65+
tcfg: NewDefaultTokenConfig(),
4166
}
4267
if len(tcfgs) > 0 {
4368
ts.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{
5172
Key: []string{"ExpiredAt"},
5273
ExpireAfter: 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{
5877
Key: []string{"ExpiredAt"},
5978
ExpireAfter: 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{
6582
Key: []string{"ExpiredAt"},
6683
ExpireAfter: time.Second * 1,
6784
})
68-
if err != nil {
69-
return
70-
}
85+
7186
store = ts
7287
return
7388
}
7489

7590
// TokenStore MongoDB storage for OAuth 2.0
7691
type TokenStore struct {
7792
tcfg *TokenConfig
78-
mcfg *Config
93+
dbName string
7994
session *mgo.Session
8095
}
8196

97+
// Close close the mongo session
98+
func (ts *TokenStore) Close() {
99+
ts.session.Close()
100+
}
101+
82102
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)
84104
}
85105

86106
func (ts *TokenStore) cHandler(name string, handler func(c *mgo.Collection)) {
87107
session := ts.session.Clone()
88108
defer session.Close()
89-
handler(session.DB(ts.mcfg.DB).C(name))
109+
handler(session.DB(ts.dbName).C(name))
90110
return
91111
}
92112

token_test.go renamed to mongo_test.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
package mongo_test
1+
package mongo
22

33
import (
44
"testing"
55
"time"
66

7-
"gopkg.in/go-oauth2/mongo.v1"
87
"gopkg.in/oauth2.v3/models"
98

109
. "github.com/smartystreets/goconvey/convey"
1110
)
1211

12+
const (
13+
url = "127.0.0.1:27017"
14+
dbName = "mydb_test"
15+
)
16+
1317
func TestTokenStore(t *testing.T) {
1418
Convey("Test mongodb token store", t, func() {
15-
mcfg := mongo.NewConfig("mongodb://127.0.0.1:27017", "oauth2")
16-
store, err := mongo.NewTokenStore(mcfg)
17-
So(err, ShouldBeNil)
19+
store := NewTokenStore(NewConfig(url, dbName))
1820

1921
Convey("Test authorization code store", func() {
2022
info := &models.Token{

0 commit comments

Comments
 (0)