Skip to content
Snippets Groups Projects
stateinfo.go 1.27 KiB
Newer Older
package state

import (
	"github.com/oidc-mytoken/server/internal/utils/singleasciiencode"
Gabriel Zachmann's avatar
Gabriel Zachmann committed
	pkgModel "github.com/oidc-mytoken/server/shared/model"
Gabriel Zachmann's avatar
Gabriel Zachmann committed
// Info is a type for holding the information encoded in a State
type Info struct {
	Native       bool
	ResponseType pkgModel.ResponseType
}

const infoAsciiLen = 2

Gabriel Zachmann's avatar
Gabriel Zachmann committed
// Encode encodes the Info into a string
func (i Info) Encode() string {
	fe := singleasciiencode.NewFlagEncoder()
	fe.Set("native", i.Native)
	flags := fe.Encode()
	responseType := singleasciiencode.EncodeNumber64(byte(i.ResponseType))
	return string([]byte{flags, responseType})
}

Gabriel Zachmann's avatar
Gabriel Zachmann committed
// Decode decodes the Info from a string
func (i *Info) Decode(s string) {
	length := len(s)
	if length < infoAsciiLen {
		return
	}
	responseType, _ := singleasciiencode.DecodeNumber64(s[length-1])
	flags := singleasciiencode.Decode(s[length-2], "native")
	i.ResponseType = pkgModel.ResponseType(responseType)
	i.Native, _ = flags.Get("native")
}

Gabriel Zachmann's avatar
Gabriel Zachmann committed
// CreateState creates a new State and ConsentCode from the passed Info
func CreateState(info Info) (*State, *ConsentCode) {
	consentCode := NewConsentCode(info)
	s := consentCode.GetState()
	return NewState(s), consentCode
}

Gabriel Zachmann's avatar
Gabriel Zachmann committed
// Parse parses a State and returns the encoded Info
func (s *State) Parse() (info Info) {
	info.Decode(s.State())
	return
}