Newer
Older
package model
import (
"encoding/json"
"fmt"
"github.com/oidc-mytoken/api/v0"
yaml "gopkg.in/yaml.v3"
)
// RestrictionKey is an enum like type for restriction keys
type RestrictionKey int
// RestrictionKeys is a slice of RestrictionKey
type RestrictionKeys []RestrictionKey
// AllRestrictionKeyStrings holds all defined RestrictionKey strings
var AllRestrictionKeyStrings = api.AllRestrictionKeys
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
var AllRestrictionKeys RestrictionKeys
func init() {
for i := 0; i < int(maxRestrictionKey); i++ {
AllRestrictionKeys = append(AllRestrictionKeys, RestrictionKey(i))
}
}
// RestrictionKeys
const ( // assert that these are in the same order as api.AllRestrictionKeys
RestrictionKeyNotBefore RestrictionKey = iota
RestrictionKeyExpiresAt
RestrictionKeyScope
RestrictionKeyAudiences
RestrictionKeyIPs
RestrictionKeyGeoIPAllow
RestrictionKeyGeoIPDisallow
RestrictionKeyUsagesAT
RestrictionKeyUsagesOther
maxRestrictionKey
)
// NewRestrictionKey creates a new RestrictionKey from the grant type string
func NewRestrictionKey(s string) RestrictionKey {
for i, f := range AllRestrictionKeyStrings {
if f == s {
return RestrictionKey(i)
}
}
return -1
}
func (rk *RestrictionKey) String() string {
if *rk < 0 || int(*rk) >= len(AllRestrictionKeys) {
return ""
}
return AllRestrictionKeyStrings[*rk]
}
// Valid checks that RestrictionKey is a defined grant type
func (rk *RestrictionKey) Valid() bool {
return *rk < maxRestrictionKey && *rk >= 0
}
// UnmarshalYAML implements the yaml.Unmarshaler interface
func (rk *RestrictionKey) UnmarshalYAML(value *yaml.Node) error {
s := value.Value
if s == "" {
return fmt.Errorf("empty value in unmarshal grant type")
}
*rk = NewRestrictionKey(s)
if !rk.Valid() {
return fmt.Errorf("value '%s' not valid for RestrictionKey", s)
}
return nil
}
// UnmarshalJSON implements the json.Unmarshaler interface
func (rk *RestrictionKey) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
*rk = NewRestrictionKey(s)
if !rk.Valid() {
return fmt.Errorf("value '%s' not valid for RestrictionKey", s)
}
return nil
}
// UnmarshalText implements the encoding.TextUnmarshaler interface
func (rk *RestrictionKey) UnmarshalText(data []byte) error {
s := string(data)
*rk = NewRestrictionKey(s)
if !rk.Valid() {
return fmt.Errorf("value '%s' not valid for RestrictionKey", s)
}
return nil
}
// MarshalJSON implements the json.Marshaler interface
func (rk RestrictionKey) MarshalJSON() ([]byte, error) {
return json.Marshal(rk.String())
}
// Has checks if a a RestrictionKey is in a RestrictionKeys
func (rks RestrictionKeys) Has(rk RestrictionKey) bool {
for _, k := range rks {
if k == rk {
return true
}
}
return false
}
// Disable subtracts the passed RestrictionKeys from this RestrictionKeys and returns the left RestrictionKeys
func (rks RestrictionKeys) Disable(disable RestrictionKeys) (left RestrictionKeys) {
for _, r := range rks {
if !disable.Has(r) {
left = append(left, r)
}
}
return
}