Newer
Older
"github.com/oidc-mytoken/server/internal/config"
"github.com/oidc-mytoken/server/internal/db"
"github.com/oidc-mytoken/server/internal/db/dbrepo/authcodeinforepo/state"
"github.com/oidc-mytoken/server/internal/db/dbrepo/mytokenrepo/transfercoderepo"
"github.com/oidc-mytoken/server/shared/mytoken/capabilities"
"github.com/oidc-mytoken/server/shared/mytoken/restrictions"
// AuthFlowInfo holds database information about a started authorization flow
AuthFlowInfoOut
PollingCode *transfercoderepo.TransferCode
}
// AuthFlowInfoOut holds database information about a started authorization flow
type AuthFlowInfoOut struct {
State *state.State
Issuer string
Restrictions restrictions.Restrictions
Capabilities capabilities.Capabilities
SubtokenCapabilities capabilities.Capabilities
Name string
}
type authFlowInfo struct {
State *state.State `db:"state_h"`
Issuer string `db:"iss"`
Restrictions restrictions.Restrictions
Capabilities capabilities.Capabilities
SubtokenCapabilities capabilities.Capabilities `db:"subtoken_capabilities"`
PollingCode db.BitBool `db:"polling_code"`
ExpiresIn int64 `db:"expires_in"`
State: i.State,
Issuer: i.Issuer,
Restrictions: i.Restrictions,
Capabilities: i.Capabilities,
SubtokenCapabilities: i.SubtokenCapabilities,
Name: db.NewNullString(i.Name),
ExpiresIn: config.Get().Features.Polling.PollingCodeExpiresAfter,
PollingCode: i.PollingCode != nil,
func (i *authFlowInfo) toAuthFlowInfo() *AuthFlowInfoOut {
return &AuthFlowInfoOut{
State: i.State,
Issuer: i.Issuer,
Restrictions: i.Restrictions,
Capabilities: i.Capabilities,
SubtokenCapabilities: i.SubtokenCapabilities,
Name: i.Name.String,
PollingCode: bool(i.PollingCode),
// Store stores the AuthFlowInfoIn in the database as well as the linked polling code if it exists
func (i *AuthFlowInfo) Store(tx *sqlx.Tx) error {
return db.RunWithinTransaction(tx, func(tx *sqlx.Tx) error {
if i.PollingCode != nil {
if err := i.PollingCode.Store(tx); err != nil {
_, err := tx.NamedExec(`INSERT INTO AuthInfo (state_h, iss, restrictions, capabilities, subtoken_capabilities, name, expires_in, polling_code) VALUES(:state_h, :iss, :restrictions, :capabilities, :subtoken_capabilities, :name, :expires_in, :polling_code)`, store)
// GetAuthFlowInfoByState returns AuthFlowInfoIn by state
func GetAuthFlowInfoByState(state *state.State) (*AuthFlowInfoOut, error) {
if err := db.Transact(func(tx *sqlx.Tx) error {
return tx.Get(&info, `SELECT state_h, iss, restrictions, capabilities, subtoken_capabilities, name, polling_code FROM AuthInfo WHERE state_h=? AND expires_at >= CURRENT_TIMESTAMP()`, state)
}); err != nil {
// DeleteAuthFlowInfoByState deletes the AuthFlowInfoIn for a given state
func DeleteAuthFlowInfoByState(tx *sqlx.Tx, state *state.State) error {
return db.RunWithinTransaction(tx, func(tx *sqlx.Tx) error {
_, err := tx.Exec(`DELETE FROM AuthInfo WHERE state_h = ?`, state)
func UpdateTokenInfoByState(tx *sqlx.Tx, state *state.State, r restrictions.Restrictions, c, sc capabilities.Capabilities) error {
return db.RunWithinTransaction(tx, func(tx *sqlx.Tx) error {
_, err := tx.Exec(`UPDATE AuthInfo SET restrictions=?, capabilities=?, subtoken_capabilities=? WHERE state_h=?`, r, c, sc, state)
return err
})
}