Mongo Storage for OAuth 2.0
This implementation of the go-oauth2 store uses the newer, officially maintained MongoDB driver for Go. Tokens are clients are each stored in a dedicated collection, the database name is specified on store creation.
$ go get -u -v github.com/tassm/oauth2-mongo-store/
package main
import (
// stuff
)
func main() {
// create a mongodb client
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI("some-connection-string"))
if err != nil {
panic(err)
}
defer client.Disconnect(context.TODO())
// create a client store and token store
clientStore = store.NewMongoClientStore(client, "my-db-name")
tokenStore = store.NewMongoTokenStore(client, "my-db-name")
// create oauth manager
manager := manage.NewDefaultManager()
// create a client with access
exampleClient = &models.Client{
//ID is generated by the DB so we omit it here...
UserID: "example",
Secret: "example",
Domain: "localhost",
}
// persist the test client to the store
clientStore.Set(client)
// use mongodb client store and token store
manager.MapClientStorage(clientStore)
manager.MapTokenStorage(tokenStore)
// ...
}
I have included a couple of basic integration tests which can be run via makefile. This requires docker and docker-compose as these integrate with a containerised image of MongoDB 5
make test
Copyright (c) 2022 Tasman Mayers