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

import (
	"database/sql"
	"log"

	"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"`
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),
	}
}

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) VALUES(?)`, i.PollingCode)
			if err != nil {
				return err
			}
			pid, err := res.LastInsertId()
			if err != nil {
				return err
			}
			upid := uint64(pid)
			store.PollingCodeID = &upid
		}
Gabriel Zachmann's avatar
Gabriel Zachmann committed
		_, err := tx.NamedExec(`INSERT INTO AuthInfo (state, iss, restrictions, capabilities, name, polling_code_id) VALUES(:state, :iss, :restrictions, :capabilities, :name, :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=?`, state); err != nil {
		return nil, err
Gabriel Zachmann's avatar
Gabriel Zachmann committed
	return info.toAuthFlowInfo(), nil