tangled
alpha
login
or
join now
willdot.net
/
tangled-fork
forked from
tangled.org/core
Monorepo for Tangled
0
fork
atom
overview
issues
pulls
pipelines
appview: auth: remove package
anirudh.fi
9 months ago
07628ed9
4d3b93c1
verified
This commit was signed with the committer's
known signature
.
anirudh.fi
SSH Key Fingerprint:
SHA256:cz35vdbiWEzCNEfuL9fMC2JVIhtXavXBHrRjv8gxpAk=
-225
2 changed files
expand all
collapse all
unified
split
appview
auth
auth.go
state
state.go
-217
appview/auth/auth.go
···
1
1
-
package auth
2
2
-
3
3
-
import (
4
4
-
"context"
5
5
-
"fmt"
6
6
-
"net/http"
7
7
-
"time"
8
8
-
9
9
-
comatproto "github.com/bluesky-social/indigo/api/atproto"
10
10
-
"github.com/bluesky-social/indigo/atproto/identity"
11
11
-
"github.com/bluesky-social/indigo/xrpc"
12
12
-
"github.com/gorilla/sessions"
13
13
-
"tangled.sh/tangled.sh/core/appview"
14
14
-
)
15
15
-
16
16
-
type Auth struct {
17
17
-
Store *sessions.CookieStore
18
18
-
}
19
19
-
20
20
-
type AtSessionCreate struct {
21
21
-
comatproto.ServerCreateSession_Output
22
22
-
PDSEndpoint string
23
23
-
}
24
24
-
25
25
-
type AtSessionRefresh struct {
26
26
-
comatproto.ServerRefreshSession_Output
27
27
-
PDSEndpoint string
28
28
-
}
29
29
-
30
30
-
func Make(secret string) (*Auth, error) {
31
31
-
store := sessions.NewCookieStore([]byte(secret))
32
32
-
return &Auth{store}, nil
33
33
-
}
34
34
-
35
35
-
func (a *Auth) CreateInitialSession(ctx context.Context, resolved *identity.Identity, appPassword string) (*comatproto.ServerCreateSession_Output, error) {
36
36
-
37
37
-
pdsUrl := resolved.PDSEndpoint()
38
38
-
client := xrpc.Client{
39
39
-
Host: pdsUrl,
40
40
-
}
41
41
-
42
42
-
atSession, err := comatproto.ServerCreateSession(ctx, &client, &comatproto.ServerCreateSession_Input{
43
43
-
Identifier: resolved.DID.String(),
44
44
-
Password: appPassword,
45
45
-
})
46
46
-
if err != nil {
47
47
-
return nil, fmt.Errorf("invalid app password")
48
48
-
}
49
49
-
50
50
-
return atSession, nil
51
51
-
}
52
52
-
53
53
-
// Sessionish is an interface that provides access to the common fields of both types.
54
54
-
type Sessionish interface {
55
55
-
GetAccessJwt() string
56
56
-
GetActive() *bool
57
57
-
GetDid() string
58
58
-
GetDidDoc() *interface{}
59
59
-
GetHandle() string
60
60
-
GetRefreshJwt() string
61
61
-
GetStatus() *string
62
62
-
}
63
63
-
64
64
-
// Create a wrapper type for ServerRefreshSession_Output
65
65
-
type RefreshSessionWrapper struct {
66
66
-
*comatproto.ServerRefreshSession_Output
67
67
-
}
68
68
-
69
69
-
func (s *RefreshSessionWrapper) GetAccessJwt() string {
70
70
-
return s.AccessJwt
71
71
-
}
72
72
-
73
73
-
func (s *RefreshSessionWrapper) GetActive() *bool {
74
74
-
return s.Active
75
75
-
}
76
76
-
77
77
-
func (s *RefreshSessionWrapper) GetDid() string {
78
78
-
return s.Did
79
79
-
}
80
80
-
81
81
-
func (s *RefreshSessionWrapper) GetDidDoc() *interface{} {
82
82
-
return s.DidDoc
83
83
-
}
84
84
-
85
85
-
func (s *RefreshSessionWrapper) GetHandle() string {
86
86
-
return s.Handle
87
87
-
}
88
88
-
89
89
-
func (s *RefreshSessionWrapper) GetRefreshJwt() string {
90
90
-
return s.RefreshJwt
91
91
-
}
92
92
-
93
93
-
func (s *RefreshSessionWrapper) GetStatus() *string {
94
94
-
return s.Status
95
95
-
}
96
96
-
97
97
-
// Create a wrapper type for ServerRefreshSession_Output
98
98
-
type CreateSessionWrapper struct {
99
99
-
*comatproto.ServerCreateSession_Output
100
100
-
}
101
101
-
102
102
-
func (s *CreateSessionWrapper) GetAccessJwt() string {
103
103
-
return s.AccessJwt
104
104
-
}
105
105
-
106
106
-
func (s *CreateSessionWrapper) GetActive() *bool {
107
107
-
return s.Active
108
108
-
}
109
109
-
110
110
-
func (s *CreateSessionWrapper) GetDid() string {
111
111
-
return s.Did
112
112
-
}
113
113
-
114
114
-
func (s *CreateSessionWrapper) GetDidDoc() *interface{} {
115
115
-
return s.DidDoc
116
116
-
}
117
117
-
118
118
-
func (s *CreateSessionWrapper) GetHandle() string {
119
119
-
return s.Handle
120
120
-
}
121
121
-
122
122
-
func (s *CreateSessionWrapper) GetRefreshJwt() string {
123
123
-
return s.RefreshJwt
124
124
-
}
125
125
-
126
126
-
func (s *CreateSessionWrapper) GetStatus() *string {
127
127
-
return s.Status
128
128
-
}
129
129
-
130
130
-
func (a *Auth) ClearSession(r *http.Request, w http.ResponseWriter) error {
131
131
-
clientSession, err := a.Store.Get(r, appview.SessionName)
132
132
-
if err != nil {
133
133
-
return fmt.Errorf("invalid session", err)
134
134
-
}
135
135
-
if clientSession.IsNew {
136
136
-
return fmt.Errorf("invalid session")
137
137
-
}
138
138
-
clientSession.Options.MaxAge = -1
139
139
-
return clientSession.Save(r, w)
140
140
-
}
141
141
-
142
142
-
func (a *Auth) StoreSession(r *http.Request, w http.ResponseWriter, atSessionish Sessionish, pdsEndpoint string) error {
143
143
-
clientSession, _ := a.Store.Get(r, appview.SessionName)
144
144
-
clientSession.Values[appview.SessionHandle] = atSessionish.GetHandle()
145
145
-
clientSession.Values[appview.SessionDid] = atSessionish.GetDid()
146
146
-
clientSession.Values[appview.SessionPds] = pdsEndpoint
147
147
-
clientSession.Values[appview.SessionAccessJwt] = atSessionish.GetAccessJwt()
148
148
-
clientSession.Values[appview.SessionRefreshJwt] = atSessionish.GetRefreshJwt()
149
149
-
clientSession.Values[appview.SessionExpiry] = time.Now().Add(time.Minute * 15).Format(time.RFC3339)
150
150
-
clientSession.Values[appview.SessionAuthenticated] = true
151
151
-
return clientSession.Save(r, w)
152
152
-
}
153
153
-
154
154
-
func (a *Auth) AuthorizedClient(r *http.Request) (*xrpc.Client, error) {
155
155
-
clientSession, err := a.Store.Get(r, "appview-session")
156
156
-
if err != nil || clientSession.IsNew {
157
157
-
return nil, err
158
158
-
}
159
159
-
160
160
-
did := clientSession.Values["did"].(string)
161
161
-
pdsUrl := clientSession.Values["pds"].(string)
162
162
-
accessJwt := clientSession.Values["accessJwt"].(string)
163
163
-
refreshJwt := clientSession.Values["refreshJwt"].(string)
164
164
-
165
165
-
client := &xrpc.Client{
166
166
-
Host: pdsUrl,
167
167
-
Auth: &xrpc.AuthInfo{
168
168
-
AccessJwt: accessJwt,
169
169
-
RefreshJwt: refreshJwt,
170
170
-
Did: did,
171
171
-
},
172
172
-
}
173
173
-
174
174
-
return client, nil
175
175
-
}
176
176
-
177
177
-
func (a *Auth) GetSession(r *http.Request) (*sessions.Session, error) {
178
178
-
return a.Store.Get(r, appview.SessionName)
179
179
-
}
180
180
-
181
181
-
func (a *Auth) GetDid(r *http.Request) string {
182
182
-
clientSession, err := a.Store.Get(r, appview.SessionName)
183
183
-
if err != nil || clientSession.IsNew {
184
184
-
return ""
185
185
-
}
186
186
-
187
187
-
return clientSession.Values[appview.SessionDid].(string)
188
188
-
}
189
189
-
190
190
-
func (a *Auth) GetHandle(r *http.Request) string {
191
191
-
clientSession, err := a.Store.Get(r, appview.SessionName)
192
192
-
if err != nil || clientSession.IsNew {
193
193
-
return ""
194
194
-
}
195
195
-
196
196
-
return clientSession.Values[appview.SessionHandle].(string)
197
197
-
}
198
198
-
199
199
-
type User struct {
200
200
-
Handle string
201
201
-
Did string
202
202
-
Pds string
203
203
-
}
204
204
-
205
205
-
func (a *Auth) GetUser(r *http.Request) *User {
206
206
-
clientSession, err := a.Store.Get(r, appview.SessionName)
207
207
-
208
208
-
if err != nil || clientSession.IsNew {
209
209
-
return nil
210
210
-
}
211
211
-
212
212
-
return &User{
213
213
-
Handle: clientSession.Values[appview.SessionHandle].(string),
214
214
-
Did: clientSession.Values[appview.SessionDid].(string),
215
215
-
Pds: clientSession.Values[appview.SessionPds].(string),
216
216
-
}
217
217
-
}
-8
appview/state/state.go
···
19
19
"github.com/go-chi/chi/v5"
20
20
"tangled.sh/tangled.sh/core/api/tangled"
21
21
"tangled.sh/tangled.sh/core/appview"
22
22
-
"tangled.sh/tangled.sh/core/appview/auth"
23
22
"tangled.sh/tangled.sh/core/appview/db"
24
23
"tangled.sh/tangled.sh/core/appview/oauth"
25
24
"tangled.sh/tangled.sh/core/appview/pages"
···
29
28
30
29
type State struct {
31
30
db *db.DB
32
32
-
auth *auth.Auth
33
31
oauth *oauth.OAuth
34
32
enforcer *rbac.Enforcer
35
33
tidClock syntax.TIDClock
···
41
39
42
40
func Make(config *appview.Config) (*State, error) {
43
41
d, err := db.Make(config.Core.DbPath)
44
44
-
if err != nil {
45
45
-
return nil, err
46
46
-
}
47
47
-
48
48
-
auth, err := auth.Make(config.Core.CookieSecret)
49
42
if err != nil {
50
43
return nil, err
51
44
}
···
89
82
90
83
state := &State{
91
84
d,
92
92
-
auth,
93
85
oauth,
94
86
enforcer,
95
87
clock,