Skip to content

go-batteries/authy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

authy

This library handles access token and refresh token creation. You can revoke the token as well.

Requirements:

  • golang
  • sqlite3 or postgres (dialects)
  • go-migrate cli to write migration files (optional)

The config looks like this:

Config{
    &DatabaseConfig{
        Dialect string
        URL string
    },
    &AppConfig{
        TokenExpiryInSec int64
        RefreshExpiryInSec int64
    }
}

How to use

You need to have a client_id, which will generally map to an unique identifier in your data models. So, for generating tokens for users, client_id will be user_id

Presently, client_id is string.

Initializing and invoking
import (

	"github.com/go-batteries/authy"
	"github.com/go-batteries/authy/pkg/config"
)

var (
    cfg config.Config
    ctx context.Context
    clientID string
)

authService := authy.NewAuthorizer(cfg)
// migrations get run here, where `tokens` table is created

token, err := authService.Create(ctx, clientID)

isValid, err := authService.Authenticate(ctx, token.AccessToken)
_, err = authService.Revoke(ctx, token.AccessToken)

newToken, err := authService.ReAuthenticate(ctx, token.AccessToken, token.RefreshToken)

You can also revert the migration as such

import (
    "database/sql"
    "github.com/go-batteries/authy/database"
)

db, err := sql.Open(/*...*/)
database.RevertMigration(ctx, db)

You can check more examples inside tests/

TODO:

  • Provide a way to change the table name

About

Authenticate with access and refresh token

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published