Skip to content
Snippets Groups Projects
consentcode.go 859 B
Newer Older
package state

import (
	"github.com/oidc-mytoken/utils/utils"

Gabriel Zachmann's avatar
Gabriel Zachmann committed
	"github.com/oidc-mytoken/server/internal/utils/hashutils"
)

const consentCodeLen = 8

Gabriel Zachmann's avatar
Gabriel Zachmann committed
// NewConsentCode creates a new ConsentCode
func NewConsentCode() *ConsentCode {
	return ConsentCodeFromStr(utils.RandReadableAlphaString(consentCodeLen))
Gabriel Zachmann's avatar
Gabriel Zachmann committed
// ConsentCode is type for the code used for giving consent to mytoken
type ConsentCode struct {
	code  string
	state string
}

func (c ConsentCode) String() string {
	return c.code
// ConsentCodeFromStr turns a consent code string into a *ConsentCode
func ConsentCodeFromStr(code string) *ConsentCode {
	return &ConsentCode{
		code: code,
Gabriel Zachmann's avatar
Gabriel Zachmann committed
// GetState returns the state linked to a ConsentCode
func (c *ConsentCode) GetState() string {
	if c.state == "" {
Gabriel Zachmann's avatar
Gabriel Zachmann committed
		c.state = hashutils.HMACBasedHash([]byte(c.code))[:stateLen]
	}
	return c.state
}