Skip to content
Snippets Groups Projects
authcodeInfo.go 2.44 KiB
Newer Older
package dbModels

import (
	"database/sql"
	"log"

	"github.com/zachmann/mytoken/internal/config"

	"github.com/jmoiron/sqlx"

	"github.com/zachmann/mytoken/internal/db"
	"github.com/zachmann/mytoken/internal/supertoken/capabilities"
	"github.com/zachmann/mytoken/internal/supertoken/restrictions"
)

type AuthFlowInfo struct {
	State        string
	Issuer       string
	Restrictions restrictions.Restrictions
	Capabilities capabilities.Capabilities
	Name         string
Gabriel Zachmann's avatar
Gabriel Zachmann committed
	PollingCode  string
}

type authFlowInfo struct {
	State         string
Gabriel Zachmann's avatar
Gabriel Zachmann committed
	Issuer        string `db:"iss"`
	Restrictions  restrictions.Restrictions
	Capabilities  capabilities.Capabilities
	Name          sql.NullString
Gabriel Zachmann's avatar
Gabriel Zachmann committed
	PollingCodeID *uint64        `db:"polling_code_id"`
	PollingCode   sql.NullString `db:"polling_code"`
	ExpiresIn     int64          `db:"expires_in"`
Gabriel Zachmann's avatar
Gabriel Zachmann committed
func (i *AuthFlowInfo) toAuthFlowInfo() *authFlowInfo {
	return &authFlowInfo{
		State:        i.State,
		Issuer:       i.Issuer,
		Restrictions: i.Restrictions,
		Capabilities: i.Capabilities,
		Name:         db.NewNullString(i.Name),
		ExpiresIn:    config.Get().Polling.PollingCodeExpiresAfter,
Gabriel Zachmann's avatar
Gabriel Zachmann committed
func (i *authFlowInfo) toAuthFlowInfo() *AuthFlowInfo {
	return &AuthFlowInfo{
		State:        i.State,
		Issuer:       i.Issuer,
		Restrictions: i.Restrictions,
		Capabilities: i.Capabilities,
		Name:         i.Name.String,
		PollingCode:  i.PollingCode.String,
	}
}

func (i *AuthFlowInfo) Store() error {
	log.Printf("Storing auth flow info")
Gabriel Zachmann's avatar
Gabriel Zachmann committed
	store := i.toAuthFlowInfo()
	return db.Transact(func(tx *sqlx.Tx) error {
Gabriel Zachmann's avatar
Gabriel Zachmann committed
		if i.PollingCode != "" {
			res, err := tx.Exec(`INSERT INTO PollingCodes (polling_code, expires_in) VALUES(?, ?)`, i.PollingCode, config.Get().Polling.PollingCodeExpiresAfter)
			if err != nil {
				return err
			}
			pid, err := res.LastInsertId()
			if err != nil {
				return err
			}
			upid := uint64(pid)
			store.PollingCodeID = &upid
		}
		_, err := tx.NamedExec(`INSERT INTO AuthInfo (state, iss, restrictions, capabilities, name, expires_in, polling_code_id) VALUES(:state, :iss, :restrictions, :capabilities, :name, :expires_in, :polling_code_id)`, store)
		return err
	})
Gabriel Zachmann's avatar
Gabriel Zachmann committed
func GetAuthCodeInfoByState(state string) (*AuthFlowInfo, error) {
	info := authFlowInfo{}
	if err := db.DB().Get(&info, `SELECT state, iss, restrictions, capabilities, name, polling_code FROM AuthInfoV WHERE state=? AND expires_at >= CURRENT_TIMESTAMP()`, state); err != nil {
Gabriel Zachmann's avatar
Gabriel Zachmann committed
		return nil, err
Gabriel Zachmann's avatar
Gabriel Zachmann committed
	return info.toAuthFlowInfo(), nil