-
Notifications
You must be signed in to change notification settings - Fork 1
/
header.go
75 lines (58 loc) · 1.36 KB
/
header.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package jwt
import (
"authelia.com/provider/oauth2/internal/consts"
)
// Headers is the jwt headers
type Headers struct {
Extra map[string]any `json:"extra"`
}
func NewHeaders() *Headers {
return &Headers{Extra: map[string]any{}}
}
// ToMap will transform the headers to a map structure
func (h *Headers) ToMap() map[string]any {
var filter = map[string]bool{consts.JSONWebTokenHeaderAlgorithm: true}
var extra = map[string]any{}
// filter known values from extra.
for k, v := range h.Extra {
if _, ok := filter[k]; !ok {
extra[k] = v
}
}
return extra
}
// Add will add a key-value pair to the extra field
func (h *Headers) Add(key string, value any) {
if h.Extra == nil {
h.Extra = make(map[string]any)
}
h.Extra[key] = value
}
// Get will get a value from the extra field based on a given key
func (h *Headers) Get(key string) any {
return h.Extra[key]
}
func (h *Headers) SetDefaultString(key, value string) {
if h.Extra == nil {
h.Extra = make(map[string]any)
}
var (
v any
s string
ok bool
)
if v, ok = h.Extra[key]; !ok {
h.Extra[key] = value
return
}
if s, ok = v.(string); ok && len(s) != 0 {
return
}
h.Extra[key] = value
}
// ToMapClaims will return a jwt-go MapClaims representation
func (h Headers) ToMapClaims() MapClaims {
return h.ToMap()
}