Skip to content

Commit

Permalink
use globalsign/mgo
Browse files Browse the repository at this point in the history
  • Loading branch information
LyricTian committed Sep 14, 2018
1 parent 80af0e5 commit 3d506d2
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 61 deletions.
15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
language: go
sudo: false
go_import_path: gopkg.in/go-oauth2/mongo.v3
go:
- 1.7
services:
- mongodb
before_install:
- go get -t -v ./...

script:
- go test -race -coverprofile=coverage.txt -covermode=atomic

after_success:
- bash <(curl -s https://codecov.io/bash)
31 changes: 16 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
# MongoDB Storage for OAuth 2.0
# Mongo Storage for [OAuth 2.0](https://github.com/go-oauth2/oauth2)

> Based on the mongodb token storage
[![License][License-Image]][License-Url]
[![ReportCard][ReportCard-Image]][ReportCard-Url]
[![GoDoc][GoDoc-Image]][GoDoc-Url]
[![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]

## Install

``` bash
$ go get -u -v gopkg.in/go-oauth2/mongo.v1
$ go get -u -v gopkg.in/go-oauth2/mongo.v3
```

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

import (
"gopkg.in/go-oauth2/mongo.v1"
"gopkg.in/go-oauth2/mongo.v3"
"gopkg.in/oauth2.v3/manage"
)

func main() {
manager := manage.NewDefaultManager()

// use mongodb token store
manager.MustTokenStorage(
manager.MapTokenStorage(
mongo.NewTokenStore(mongo.NewConfig(
"mongodb://127.0.0.1:27017",
"oauth2",
Expand All @@ -41,9 +38,13 @@ func main() {
Copyright (c) 2016 Lyric
```

[License-Url]: http://opensource.org/licenses/MIT
[License-Image]: https://img.shields.io/npm/l/express.svg
[ReportCard-Url]: https://goreportcard.com/report/github.com/go-oauth2/mongo
[ReportCard-Image]: https://goreportcard.com/badge/github.com/go-oauth2/mongo
[GoDoc-Url]: https://godoc.org/github.com/go-oauth2/mongo
[GoDoc-Image]: https://godoc.org/github.com/go-oauth2/mongo?status.svg
[Build-Status-Url]: https://travis-ci.org/go-oauth2/mongo
[Build-Status-Image]: https://travis-ci.org/go-oauth2/mongo.svg?branch=master
[codecov-url]: https://codecov.io/gh/go-oauth2/mongo
[codecov-image]: https://codecov.io/gh/go-oauth2/mongo/branch/master/graph/badge.svg
[reportcard-url]: https://goreportcard.com/report/gopkg.in/go-oauth2/mongo.v3
[reportcard-image]: https://goreportcard.com/badge/gopkg.in/go-oauth2/mongo.v3
[godoc-url]: https://godoc.org/gopkg.in/go-oauth2/mongo.v3
[godoc-image]: https://godoc.org/gopkg.in/go-oauth2/mongo.v3?status.svg
[license-url]: http://opensource.org/licenses/MIT
[license-image]: https://img.shields.io/npm/l/express.svg
15 changes: 0 additions & 15 deletions config.go

This file was deleted.

72 changes: 46 additions & 26 deletions token.go → mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,27 @@ import (
"encoding/json"
"time"

"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"gopkg.in/mgo.v2/txn"
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
"github.com/globalsign/mgo/txn"
"gopkg.in/oauth2.v3"
"gopkg.in/oauth2.v3/models"
)

// Config mongodb configuration parameters
type Config struct {
URL string
DB string
}

// NewConfig create mongodb configuration
func NewConfig(url, db string) *Config {
return &Config{
URL: url,
DB: db,
}
}

// TokenConfig token configuration parameters
type TokenConfig struct {
// store txn collection name(The default is oauth2)
Expand All @@ -34,59 +48,65 @@ func NewDefaultTokenConfig() *TokenConfig {
}

// NewTokenStore create a token store instance based on mongodb
func NewTokenStore(cfg *Config, tcfgs ...*TokenConfig) (store oauth2.TokenStore, err error) {
func NewTokenStore(cfg *Config, tcfgs ...*TokenConfig) (store *TokenStore) {
session, err := mgo.Dial(cfg.URL)
if err != nil {
panic(err)
}

return NewTokenStoreWithSession(session, cfg.DB, tcfgs...)
}

// NewTokenStoreWithSession create a token store instance based on mongodb
func NewTokenStoreWithSession(session *mgo.Session, dbName string, tcfgs ...*TokenConfig) (store *TokenStore) {
ts := &TokenStore{
mcfg: cfg,
tcfg: NewDefaultTokenConfig(),
dbName: dbName,
session: session,
tcfg: NewDefaultTokenConfig(),
}
if len(tcfgs) > 0 {
ts.tcfg = tcfgs[0]
}
session, err := mgo.Dial(ts.mcfg.URL)
if err != nil {
return
}
ts.session = session
err = ts.c(ts.tcfg.BasicCName).EnsureIndex(mgo.Index{

ts.c(ts.tcfg.BasicCName).EnsureIndex(mgo.Index{
Key: []string{"ExpiredAt"},
ExpireAfter: time.Second * 1,
})
if err != nil {
return
}
err = ts.c(ts.tcfg.AccessCName).EnsureIndex(mgo.Index{

ts.c(ts.tcfg.AccessCName).EnsureIndex(mgo.Index{
Key: []string{"ExpiredAt"},
ExpireAfter: time.Second * 1,
})
if err != nil {
return
}
err = ts.c(ts.tcfg.RefreshCName).EnsureIndex(mgo.Index{

ts.c(ts.tcfg.RefreshCName).EnsureIndex(mgo.Index{
Key: []string{"ExpiredAt"},
ExpireAfter: time.Second * 1,
})
if err != nil {
return
}

store = ts
return
}

// TokenStore MongoDB storage for OAuth 2.0
type TokenStore struct {
tcfg *TokenConfig
mcfg *Config
dbName string
session *mgo.Session
}

// Close close the mongo session
func (ts *TokenStore) Close() {
ts.session.Close()
}

func (ts *TokenStore) c(name string) *mgo.Collection {
return ts.session.DB(ts.mcfg.DB).C(name)
return ts.session.DB(ts.dbName).C(name)
}

func (ts *TokenStore) cHandler(name string, handler func(c *mgo.Collection)) {
session := ts.session.Clone()
defer session.Close()
handler(session.DB(ts.mcfg.DB).C(name))
handler(session.DB(ts.dbName).C(name))
return
}

Expand Down
12 changes: 7 additions & 5 deletions token_test.go → mongo_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package mongo_test
package mongo

import (
"testing"
"time"

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

. "github.com/smartystreets/goconvey/convey"
)

const (
url = "127.0.0.1:27017"
dbName = "mydb_test"
)

func TestTokenStore(t *testing.T) {
Convey("Test mongodb token store", t, func() {
mcfg := mongo.NewConfig("mongodb://127.0.0.1:27017", "oauth2")
store, err := mongo.NewTokenStore(mcfg)
So(err, ShouldBeNil)
store := NewTokenStore(NewConfig(url, dbName))

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

0 comments on commit 3d506d2

Please sign in to comment.