-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathmodels.go
More file actions
148 lines (134 loc) · 3.9 KB
/
models.go
File metadata and controls
148 lines (134 loc) · 3.9 KB
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
144
145
146
147
148
package models
import (
"encoding/json"
"net/url"
"time"
"github.com/ory/fosite"
)
type Request struct {
ID string
RequestedAt time.Time
Client *Client
RequestedScope []string
GrantedScope []string
Form map[string][]string
RequestedAudience []string
GrantedAudience []string
RotatedAt time.Time
SessionData json.RawMessage `json:",omitempty"`
}
type Client struct {
ID string
Secret string
RotatedSecrets []string
RedirectURIs []string
GrantTypes []string
ResponseTypes []string
Scopes []string
Audience []string
Public bool
}
type AuthorizeRequest struct {
ResponseTypes []string
RedirectURI string
State string
HandledResponseTypes []string
ResponseMode string
DefaultResponseMode string
Request *Request
}
func FromFositeReq(reqester fosite.Requester) *Request {
req := reqester.(*fosite.Request)
r := &Request{
ID: req.ID,
RequestedAt: req.RequestedAt,
Client: FromFositeClient(req.Client),
RequestedScope: req.RequestedScope,
GrantedScope: req.GrantedScope,
Form: req.Form,
RequestedAudience: req.RequestedAudience,
GrantedAudience: req.GrantedAudience,
}
if sess := req.GetSession(); sess != nil {
if data, err := json.Marshal(sess); err == nil {
r.SessionData = data
}
}
return r
}
func (r *Request) ToFositeReq() *fosite.Request {
return &fosite.Request{
ID: r.ID,
RequestedAt: r.RequestedAt,
Client: r.Client.ToFositeClient(),
RequestedScope: r.RequestedScope,
GrantedScope: r.GrantedScope,
Form: r.Form,
RequestedAudience: r.RequestedAudience,
GrantedAudience: r.GrantedAudience,
}
}
func FromFositeClient(client fosite.Client) *Client {
c := client.(*fosite.DefaultClient)
var rotatedSecrets []string
for _, rotSecret := range c.RotatedSecrets {
rotatedSecrets = append(rotatedSecrets, string(rotSecret))
}
return &Client{
ID: c.ID,
Secret: string(c.Secret),
RotatedSecrets: rotatedSecrets,
RedirectURIs: c.RedirectURIs,
GrantTypes: c.GrantTypes,
ResponseTypes: c.ResponseTypes,
Scopes: c.Scopes,
Audience: c.Audience,
Public: c.Public,
}
}
func (c *Client) ToFositeClient() *fosite.DefaultClient {
var rotatedSecrets [][]byte
for _, rotSecret := range c.RotatedSecrets {
rotatedSecrets = append(rotatedSecrets, []byte(rotSecret))
}
return &fosite.DefaultClient{
ID: c.ID,
Secret: []byte(c.Secret),
RotatedSecrets: rotatedSecrets,
RedirectURIs: c.RedirectURIs,
GrantTypes: c.GrantTypes,
ResponseTypes: c.ResponseTypes,
Scopes: c.Scopes,
Audience: c.Audience,
Public: c.Public,
}
}
func FromFositeAuthorizeRequest(reqester fosite.AuthorizeRequester) *AuthorizeRequest {
req := reqester.(*fosite.AuthorizeRequest)
return &AuthorizeRequest{
ResponseTypes: req.ResponseTypes,
RedirectURI: req.RedirectURI.String(),
State: req.State,
HandledResponseTypes: req.HandledResponseTypes,
ResponseMode: string(req.ResponseMode),
DefaultResponseMode: string(req.DefaultResponseMode),
Request: FromFositeReq(&req.Request),
}
}
func (ar *AuthorizeRequest) ToFositeAuthorizeRequest() *fosite.AuthorizeRequest {
return &fosite.AuthorizeRequest{
ResponseTypes: ar.ResponseTypes,
RedirectURI: Must(url.Parse(ar.RedirectURI)),
State: ar.State,
HandledResponseTypes: ar.HandledResponseTypes,
ResponseMode: fosite.ResponseModeType(ar.ResponseMode),
DefaultResponseMode: fosite.ResponseModeType(ar.DefaultResponseMode),
Request: *ar.Request.ToFositeReq(),
}
}
func Must[T any](value T, err error) T {
if err != nil {
panic(err)
}
return value
}