Debian Codegraph @

pkg/oidc/types.go

raw
1 package oidc
2
3 import (
4 "database/sql/driver"
5 "encoding/json"
6 "errors"
7 "fmt"
8 "reflect"
9 "strings"
10 "time"
11
12 jose "github.com/go-jose/go-jose/v4"
13 "github.com/muhlemmer/gu"
14 "github.com/zitadel/schema"
15 "golang.org/x/text/language"
16 )
17
18 type Audience []string
19
20 func (a *Audience) UnmarshalJSON(text []byte) error {
21 var i any
22 err := json.Unmarshal(text, &i)
23 if err != nil {
24 return err
25 }
26 switch aud := i.(type) {
27 case []any:
28 *a = make([]string, len(aud))
29 for i, audience := range aud {
30 (*a)[i] = audience.(string)
31 }
32 case string:
33 *a = []string{aud}
34 }
35 return nil
36 }
37
38 type AuthenticationMethodsReferences []string
39
40 func (a *AuthenticationMethodsReferences) UnmarshalJSON(data []byte) error {
41 var dst any
42 if err := json.Unmarshal(data, &dst); err != nil {
43 return fmt.Errorf("oidc amr: %w", err)
44 }
45
46 switch v := dst.(type) {
47 case nil:
48 *a = nil
49 case string:
50 *a = AuthenticationMethodsReferences{v}
51 case []any:
52 refs, err := gu.AssertInterfaces[string](v)
53 if err != nil {
54 return fmt.Errorf("oidc amr: %w", err)
55 }
56 *a = AuthenticationMethodsReferences(refs)
57 default:
58 return fmt.Errorf("oidc amr: unsupported type: %T", v)
59 }
60 return nil
61 }
62
63 type Display string
64
65 func (d *Display) UnmarshalText(text []byte) error {
66 display := Display(text)
67 switch display {
68 case DisplayPage, DisplayPopup, DisplayTouch, DisplayWAP:
69 *d = display
70 }
71 return nil
72 }
73
74 type Gender string
75
76 type Locale struct {
77 tag language.Tag
78 }
79
80 func NewLocale(tag language.Tag) *Locale {
81 return &Locale{tag: tag}
82 }
83
84 func (l *Locale) Tag() language.Tag {
85 if l == nil {
86 return language.Und
87 }
88
89 return l.tag
90 }
91
92 func (l *Locale) String() string {
93 return l.Tag().String()
94 }
95
96 func (l *Locale) MarshalJSON() ([]byte, error) {
97 tag := l.Tag()
98 if tag.IsRoot() {
99 return []byte("null"), nil
100 }
101
102 return json.Marshal(tag)
103 }
104
105 // UnmarshalJSON implements json.Unmarshaler.
106 // When [language.ValueError] is encountered, the containing tag will be set
107 // to an empty value (language "und") and no error will be returned.
108 // This state can be checked with the `l.Tag().IsRoot()` method.
109 func (l *Locale) UnmarshalJSON(data []byte) error {
110 if len(data) == 0 || string(data) == "\"\"" {
111 return nil
112 }
113 err := json.Unmarshal(data, &l.tag)
114 if err == nil {
115 return nil
116 }
117
118 // catch "well-formed but unknown" errors
119 var target language.ValueError
120 if errors.As(err, &target) {
121 l.tag = language.Tag{}
122 return nil
123 }
124 return err
125 }
126
127 type Locales []language.Tag
128
129 // ParseLocales parses a slice of strings into Locales.
130 // If an entry causes a parse error or is undefined,
131 // it is ignored and not set to Locales.
132 func ParseLocales(locales []string) Locales {
133 out := make(Locales, 0, len(locales))
134 for _, locale := range locales {
135 tag, err := language.Parse(locale)
136 if err == nil && !tag.IsRoot() {
137 out = append(out, tag)
138 }
139 }
140 return out
141 }
142
143 func (l Locales) String() string {
144 tags := make([]string, len(l))
145 for i, tag := range l {
146 tags[i] = tag.String()
147 }
148 return strings.Join(tags, " ")
149 }
150
151 // UnmarshalText implements the [encoding.TextUnmarshaler] interface.
152 // It decodes an unquoted space separated string into Locales.
153 // Undefined language tags in the input are ignored and omitted from
154 // the resulting Locales.
155 func (l *Locales) UnmarshalText(text []byte) error {
156 *l = ParseLocales(
157 strings.Split(string(text), " "),
158 )
159 return nil
160 }
161
162 // UnmarshalJSON implements the [json.Unmarshaler] interface.
163 // It decodes a json array or a space separated string into Locales.
164 // Undefined language tags in the input are ignored and omitted from
165 // the resulting Locales.
166 func (l *Locales) UnmarshalJSON(data []byte) error {
167 var dst any
168 if err := json.Unmarshal(data, &dst); err != nil {
169 return fmt.Errorf("oidc locales: %w", err)
170 }
171
172 // We catch the possibility of a space separated string here,
173 // because UnmarshalText might have been implicitly called
174 // by the json library before we added UnmarshalJSON.
175 switch v := dst.(type) {
176 case nil:
177 *l = nil
178 case string:
179 *l = ParseLocales(strings.Split(v, " "))
180 case []any:
181 locales, err := gu.AssertInterfaces[string](v)
182 if err != nil {
183 return fmt.Errorf("oidc locales: %w", err)
184 }
185 *l = ParseLocales(locales)
186 default:
187 return fmt.Errorf("oidc locales: unsupported type: %T", v)
188 }
189 return nil
190 }
191
192 type MaxAge *uint
193
194 func NewMaxAge(i uint) MaxAge {
195 return &i
196 }
197
198 type SpaceDelimitedArray []string
199
200 type Prompt SpaceDelimitedArray
201
202 type ResponseType string
203
204 type ResponseMode string
205
206 func (s SpaceDelimitedArray) String() string {
207 return strings.Join(s, " ")
208 }
209
210 func (s *SpaceDelimitedArray) UnmarshalText(text []byte) error {
211 *s = strings.Split(string(text), " ")
212 return nil
213 }
214
215 func (s SpaceDelimitedArray) MarshalText() ([]byte, error) {
216 return []byte(s.String()), nil
217 }
218
219 func (s SpaceDelimitedArray) MarshalJSON() ([]byte, error) {
220 return json.Marshal((s).String())
221 }
222
223 func (s *SpaceDelimitedArray) UnmarshalJSON(data []byte) error {
224 var str string
225 if err := json.Unmarshal(data, &str); err != nil {
226 return err
227 }
228 *s = strings.Split(str, " ")
229 return nil
230 }
231
232 func (s *SpaceDelimitedArray) Scan(src any) error {
233 if src == nil {
234 *s = nil
235 return nil
236 }
237 switch v := src.(type) {
238 case string:
239 if len(v) == 0 {
240 *s = SpaceDelimitedArray{}
241 return nil
242 }
243 *s = strings.Split(v, " ")
244 case []byte:
245 if len(v) == 0 {
246 *s = SpaceDelimitedArray{}
247 return nil
248 }
249 *s = strings.Split(string(v), " ")
250 default:
251 return fmt.Errorf("cannot convert %T to SpaceDelimitedArray", src)
252 }
253 return nil
254 }
255
256 func (s SpaceDelimitedArray) Value() (driver.Value, error) {
257 return strings.Join(s, " "), nil
258 }
259
260 // NewEncoder returns a schema Encoder with
261 // a registered encoder for SpaceDelimitedArray.
262 func NewEncoder() *schema.Encoder {
263 e := schema.NewEncoder()
264 e.RegisterEncoder(SpaceDelimitedArray{}, func(value reflect.Value) string {
265 return value.Interface().(SpaceDelimitedArray).String()
266 })
267 e.RegisterEncoder(Locales{}, func(value reflect.Value) string {
268 return value.Interface().(Locales).String()
269 })
270 return e
271 }
272
273 type Time int64
274
275 func (ts Time) AsTime() time.Time {
276 if ts == 0 {
277 return time.Time{}
278 }
279 return time.Unix(int64(ts), 0)
280 }
281
282 func FromTime(tt time.Time) Time {
283 if tt.IsZero() {
284 return 0
285 }
286 return Time(tt.Unix())
287 }
288
289 func NowTime() Time {
290 return FromTime(time.Now())
291 }
292
293 func (ts *Time) UnmarshalJSON(data []byte) error {
294 var v any
295 if err := json.Unmarshal(data, &v); err != nil {
296 return fmt.Errorf("oidc.Time: %w", err)
297 }
298 switch x := v.(type) {
299 case float64:
300 *ts = Time(x)
301 case string:
302 // Compatibility with Auth0:
303 // https://github.com/zitadel/oidc/issues/292
304 tt, err := time.Parse(time.RFC3339, x)
305 if err != nil {
306 return fmt.Errorf("oidc.Time: %w", err)
307 }
308 *ts = FromTime(tt)
309 case nil:
310 *ts = 0
311 default:
312 return fmt.Errorf("oidc.Time: unable to parse type %T with value %v", x, x)
313 }
314 return nil
315 }
316
317 type RequestObject struct {
318 Issuer string `json:"iss"`
319 Audience Audience `json:"aud"`
320 AuthRequest
321 }
322
323 func (r *RequestObject) GetIssuer() string {
324 return r.Issuer
325 }
326
327 func (*RequestObject) SetSignatureAlgorithm(algorithm jose.SignatureAlgorithm) {}