Skip to content
Snippets Groups Projects
oidcFlow.go 1.98 KiB
Newer Older
Gabriel Zachmann's avatar
Gabriel Zachmann committed
package model

import (
	"encoding/json"
Gabriel Zachmann's avatar
Gabriel Zachmann committed
	"github.com/oidc-mytoken/api/v0"
	"github.com/pkg/errors"
	"gopkg.in/yaml.v3"
// OIDCFlow is a enum like type for oidc flows
Gabriel Zachmann's avatar
Gabriel Zachmann committed
type OIDCFlow int

Gabriel Zachmann's avatar
Gabriel Zachmann committed
var oidcFlows = [...]string{api.OIDCFlowAuthorizationCode}
Gabriel Zachmann's avatar
Gabriel Zachmann committed

// OIDCFlows
const (
	OIDCFlowAuthorizationCode OIDCFlow = iota
Gabriel Zachmann's avatar
Gabriel Zachmann committed
	// OIDCFlowDevice
Gabriel Zachmann's avatar
Gabriel Zachmann committed
	maxFlow
)

// NewOIDCFlow creates a new OIDCFlow from the flow string
Gabriel Zachmann's avatar
Gabriel Zachmann committed
func NewOIDCFlow(s string) OIDCFlow {
	for i, f := range oidcFlows {
		if f == s {
			return OIDCFlow(i)
		}
	}
	return -1
}

Gabriel Zachmann's avatar
Gabriel Zachmann committed
func (f *OIDCFlow) String() string {
Gabriel Zachmann's avatar
Gabriel Zachmann committed
	if *f < 0 || int(*f) >= len(oidcFlows) {
		return ""
	}
	return oidcFlows[*f]
}

// Valid checks that OIDCFlow is a defined flow
func (f *OIDCFlow) Valid() bool {
	return *f < maxFlow && *f >= 0
}

// UnmarshalYAML implements the yaml.Unmarshaler interface
func (f *OIDCFlow) UnmarshalYAML(value *yaml.Node) error {
	s := value.Value
	if s == "" {
		return errors.New("empty value in unmarshal oidc flow")
Gabriel Zachmann's avatar
Gabriel Zachmann committed
	}
	*f = NewOIDCFlow(s)
	if !f.Valid() {
		return errors.Errorf("value '%s' not valid for OIDCFlow", s)
Gabriel Zachmann's avatar
Gabriel Zachmann committed
	}
	return nil
}

// UnmarshalJSON implements the json.Unmarshaler interface
Gabriel Zachmann's avatar
Gabriel Zachmann committed
func (f *OIDCFlow) UnmarshalJSON(data []byte) error {
	var s string
	if err := errors.WithStack(json.Unmarshal(data, &s)); err != nil {
Gabriel Zachmann's avatar
Gabriel Zachmann committed
		return err
	}
	*f = NewOIDCFlow(s)
	if !f.Valid() {
		return errors.Errorf("value '%s' not valid for OIDCFlow", s)
Gabriel Zachmann's avatar
Gabriel Zachmann committed
	}
	return nil
}

// MarshalJSON implements the json.Marshaler interface
func (f OIDCFlow) MarshalJSON() ([]byte, error) {
	data, err := json.Marshal(f.String())
	return data, errors.WithStack(err)
Gabriel Zachmann's avatar
Gabriel Zachmann committed
}

// AddToSliceIfNotFound adds the OIDCFlow to a slice s if it is not already there
func (f OIDCFlow) AddToSliceIfNotFound(s *[]OIDCFlow) {
	if OIDCFlowIsInSlice(f, *s) {
	*s = append(*s, f)
// OIDCFlowIsInSlice checks if a OIDCFlow is present in a slice of OIDCFlows
func OIDCFlowIsInSlice(f OIDCFlow, s []OIDCFlow) bool {
Gabriel Zachmann's avatar
Gabriel Zachmann committed
	for _, ss := range s {
Gabriel Zachmann's avatar
Gabriel Zachmann committed
		if ss == f {
	return false
Gabriel Zachmann's avatar
Gabriel Zachmann committed
}