-
Notifications
You must be signed in to change notification settings - Fork 20
/
jwt_templates.go
143 lines (118 loc) · 3.79 KB
/
jwt_templates.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package clerk
import (
"encoding/json"
"fmt"
"net/http"
)
type JWTTemplatesService service
type JWTTemplate struct {
ID string `json:"id"`
Object string `json:"object"`
Name string `json:"name"`
Claims json.RawMessage `json:"claims"`
Lifetime int `json:"lifetime"`
AllowedClockSkew int `json:"allowed_clock_skew"`
CustomSigningKey bool `json:"custom_signing_key"`
SigningAlgorithm string `json:"signing_algorithm"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
}
type CreateUpdateJWTTemplate struct {
Name string `json:"name"`
Claims map[string]interface{} `json:"claims"`
Lifetime *int `json:"lifetime"`
AllowedClockSkew *int `json:"allowed_clock_skew"`
CustomSigningKey bool `json:"custom_signing_key"`
SigningAlgorithm *string `json:"signing_algorithm"`
SigningKey *string `json:"signing_key"`
}
func (t CreateUpdateJWTTemplate) toRequest() (*createUpdateJWTTemplateRequest, error) {
claimsBytes, err := json.Marshal(t.Claims)
if err != nil {
return nil, err
}
return &createUpdateJWTTemplateRequest{
Claims: string(claimsBytes),
Name: t.Name,
Lifetime: t.Lifetime,
AllowedClockSkew: t.AllowedClockSkew,
CustomSigningKey: t.CustomSigningKey,
SigningAlgorithm: t.SigningAlgorithm,
SigningKey: t.SigningKey,
}, nil
}
type createUpdateJWTTemplateRequest struct {
Claims string `json:"claims"`
Name string `json:"name"`
Lifetime *int `json:"lifetime,omitempty"`
AllowedClockSkew *int `json:"allowed_clock_skew,omitempty"`
CustomSigningKey bool `json:"custom_signing_key"`
SigningAlgorithm *string `json:"signing_algorithm,omitempty"`
SigningKey *string `json:"signing_key,omitempty"`
}
func (s JWTTemplatesService) ListAll() ([]JWTTemplate, error) {
req, err := s.client.NewRequest(http.MethodGet, JWTTemplatesUrl)
if err != nil {
return nil, err
}
jwtTemplates := make([]JWTTemplate, 0)
if _, err = s.client.Do(req, &jwtTemplates); err != nil {
return nil, err
}
return jwtTemplates, nil
}
func (s JWTTemplatesService) Read(id string) (*JWTTemplate, error) {
reqURL := fmt.Sprintf("%s/%s", JWTTemplatesUrl, id)
req, err := s.client.NewRequest(http.MethodGet, reqURL)
if err != nil {
return nil, err
}
jwtTemplate := &JWTTemplate{}
if _, err = s.client.Do(req, jwtTemplate); err != nil {
return nil, err
}
return jwtTemplate, nil
}
func (s JWTTemplatesService) Create(params *CreateUpdateJWTTemplate) (*JWTTemplate, error) {
reqBody, err := params.toRequest()
if err != nil {
return nil, err
}
req, err := s.client.NewRequest(http.MethodPost, JWTTemplatesUrl, reqBody)
if err != nil {
return nil, err
}
resp := JWTTemplate{}
if _, err = s.client.Do(req, &resp); err != nil {
return nil, err
}
return &resp, nil
}
func (s JWTTemplatesService) Update(id string, params *CreateUpdateJWTTemplate) (*JWTTemplate, error) {
reqURL := fmt.Sprintf("%s/%s", JWTTemplatesUrl, id)
reqBody, err := params.toRequest()
if err != nil {
return nil, err
}
req, err := s.client.NewRequest(http.MethodPatch, reqURL, reqBody)
if err != nil {
return nil, err
}
resp := JWTTemplate{}
if _, err = s.client.Do(req, &resp); err != nil {
return nil, err
}
return &resp, nil
}
func (s JWTTemplatesService) Delete(id string) (*DeleteResponse, error) {
reqURL := fmt.Sprintf("%s/%s", JWTTemplatesUrl, id)
req, err := s.client.NewRequest(http.MethodDelete, reqURL)
if err != nil {
return nil, err
}
resp := &DeleteResponse{}
if _, err = s.client.Do(req, resp); err != nil {
return nil, err
}
return resp, nil
}