Skip to content

A datastore implementation for go-oauth2 using the official MongoDB driver for Go

Notifications You must be signed in to change notification settings

tassm/oauth2-mongo-store

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

Install

$ go get -u -v github.com/tassm/oauth2-mongo-store/

Usage

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)
	// ...
}

Tests

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

MIT License

Copyright (c) 2022 Tasman Mayers

About

A datastore implementation for go-oauth2 using the official MongoDB driver for Go

Resources

Stars

Watchers

Forks

Packages

No packages published