authy

package module
v0.0.0-...-e934472 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 28, 2023 License: MIT Imports: 15 Imported by: 0

README

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewAuthorizer

func NewAuthorizer(cfg config.Config) (tokens.Service, error)

Types

This section is empty.

Directories

Path Synopsis
internal
pkg/bob
simplest package ever to put the table name in place Usage bob.Table(&User{}).Build(`SELECT * FROM :table`)
simplest package ever to put the table name in place Usage bob.Table(&User{}).Build(`SELECT * FROM :table`)
pkg
src

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL