Skip to content
Snippets Groups Projects
consentcode.go 1.11 KiB
Newer Older
package state

import (
	"github.com/oidc-mytoken/server/internal/utils/hashUtils"
	"github.com/oidc-mytoken/server/shared/utils"
)

const stateLen = 16
const consentCodeLen = 8

Gabriel Zachmann's avatar
Gabriel Zachmann committed
// NewConsentCode creates a new ConsentCode
func NewConsentCode(info Info) *ConsentCode {
	return &ConsentCode{
		r:           utils.RandASCIIString(consentCodeLen),
		encodedInfo: info.Encode(),
	}
}

Gabriel Zachmann's avatar
Gabriel Zachmann committed
// ParseConsentCode parses a string into a ConsentCode
func ParseConsentCode(cc string) *ConsentCode {
	return &ConsentCode{
		r:           cc[:len(cc)-infoAsciiLen],
		encodedInfo: cc[len(cc)-infoAsciiLen:],
		public:      cc,
	}
}

Gabriel Zachmann's avatar
Gabriel Zachmann committed
// ConsentCode is type for the code used for giving consent to mytoken
type ConsentCode struct {
	r           string
	encodedInfo string
	public      string
	state       string
}

func (c *ConsentCode) String() string {
	if c.public == "" {
		c.public = c.r + c.encodedInfo
	}
	return c.public
}

Gabriel Zachmann's avatar
Gabriel Zachmann committed
// GetState returns the state linked to a ConsentCode
func (c *ConsentCode) GetState() string {
	if c.state == "" {
		c.state = hashUtils.HMACSHA3Str([]byte("state"), []byte(c.r))[:stateLen] + c.encodedInfo
	}
	return c.state
}