···
9
-
comatproto "github.com/bluesky-social/indigo/api/atproto"
10
-
"github.com/bluesky-social/indigo/atproto/identity"
11
-
"github.com/bluesky-social/indigo/xrpc"
12
-
"github.com/gorilla/sessions"
13
-
"tangled.sh/tangled.sh/core/appview"
17
-
Store *sessions.CookieStore
20
-
type AtSessionCreate struct {
21
-
comatproto.ServerCreateSession_Output
25
-
type AtSessionRefresh struct {
26
-
comatproto.ServerRefreshSession_Output
30
-
func Make(secret string) (*Auth, error) {
31
-
store := sessions.NewCookieStore([]byte(secret))
32
-
return &Auth{store}, nil
35
-
func (a *Auth) CreateInitialSession(ctx context.Context, resolved *identity.Identity, appPassword string) (*comatproto.ServerCreateSession_Output, error) {
37
-
pdsUrl := resolved.PDSEndpoint()
38
-
client := xrpc.Client{
42
-
atSession, err := comatproto.ServerCreateSession(ctx, &client, &comatproto.ServerCreateSession_Input{
43
-
Identifier: resolved.DID.String(),
44
-
Password: appPassword,
47
-
return nil, fmt.Errorf("invalid app password")
50
-
return atSession, nil
53
-
// Sessionish is an interface that provides access to the common fields of both types.
54
-
type Sessionish interface {
55
-
GetAccessJwt() string
58
-
GetDidDoc() *interface{}
60
-
GetRefreshJwt() string
64
-
// Create a wrapper type for ServerRefreshSession_Output
65
-
type RefreshSessionWrapper struct {
66
-
*comatproto.ServerRefreshSession_Output
69
-
func (s *RefreshSessionWrapper) GetAccessJwt() string {
73
-
func (s *RefreshSessionWrapper) GetActive() *bool {
77
-
func (s *RefreshSessionWrapper) GetDid() string {
81
-
func (s *RefreshSessionWrapper) GetDidDoc() *interface{} {
85
-
func (s *RefreshSessionWrapper) GetHandle() string {
89
-
func (s *RefreshSessionWrapper) GetRefreshJwt() string {
93
-
func (s *RefreshSessionWrapper) GetStatus() *string {
97
-
// Create a wrapper type for ServerRefreshSession_Output
98
-
type CreateSessionWrapper struct {
99
-
*comatproto.ServerCreateSession_Output
102
-
func (s *CreateSessionWrapper) GetAccessJwt() string {
106
-
func (s *CreateSessionWrapper) GetActive() *bool {
110
-
func (s *CreateSessionWrapper) GetDid() string {
114
-
func (s *CreateSessionWrapper) GetDidDoc() *interface{} {
118
-
func (s *CreateSessionWrapper) GetHandle() string {
122
-
func (s *CreateSessionWrapper) GetRefreshJwt() string {
123
-
return s.RefreshJwt
126
-
func (s *CreateSessionWrapper) GetStatus() *string {
130
-
func (a *Auth) ClearSession(r *http.Request, w http.ResponseWriter) error {
131
-
clientSession, err := a.Store.Get(r, appview.SessionName)
133
-
return fmt.Errorf("invalid session", err)
135
-
if clientSession.IsNew {
136
-
return fmt.Errorf("invalid session")
138
-
clientSession.Options.MaxAge = -1
139
-
return clientSession.Save(r, w)
142
-
func (a *Auth) StoreSession(r *http.Request, w http.ResponseWriter, atSessionish Sessionish, pdsEndpoint string) error {
143
-
clientSession, _ := a.Store.Get(r, appview.SessionName)
144
-
clientSession.Values[appview.SessionHandle] = atSessionish.GetHandle()
145
-
clientSession.Values[appview.SessionDid] = atSessionish.GetDid()
146
-
clientSession.Values[appview.SessionPds] = pdsEndpoint
147
-
clientSession.Values[appview.SessionAccessJwt] = atSessionish.GetAccessJwt()
148
-
clientSession.Values[appview.SessionRefreshJwt] = atSessionish.GetRefreshJwt()
149
-
clientSession.Values[appview.SessionExpiry] = time.Now().Add(time.Minute * 15).Format(time.RFC3339)
150
-
clientSession.Values[appview.SessionAuthenticated] = true
151
-
return clientSession.Save(r, w)
154
-
func (a *Auth) AuthorizedClient(r *http.Request) (*xrpc.Client, error) {
155
-
clientSession, err := a.Store.Get(r, "appview-session")
156
-
if err != nil || clientSession.IsNew {
160
-
did := clientSession.Values["did"].(string)
161
-
pdsUrl := clientSession.Values["pds"].(string)
162
-
accessJwt := clientSession.Values["accessJwt"].(string)
163
-
refreshJwt := clientSession.Values["refreshJwt"].(string)
165
-
client := &xrpc.Client{
167
-
Auth: &xrpc.AuthInfo{
168
-
AccessJwt: accessJwt,
169
-
RefreshJwt: refreshJwt,
177
-
func (a *Auth) GetSession(r *http.Request) (*sessions.Session, error) {
178
-
return a.Store.Get(r, appview.SessionName)
181
-
func (a *Auth) GetDid(r *http.Request) string {
182
-
clientSession, err := a.Store.Get(r, appview.SessionName)
183
-
if err != nil || clientSession.IsNew {
187
-
return clientSession.Values[appview.SessionDid].(string)
190
-
func (a *Auth) GetHandle(r *http.Request) string {
191
-
clientSession, err := a.Store.Get(r, appview.SessionName)
192
-
if err != nil || clientSession.IsNew {
196
-
return clientSession.Values[appview.SessionHandle].(string)
205
-
func (a *Auth) GetUser(r *http.Request) *User {
206
-
clientSession, err := a.Store.Get(r, appview.SessionName)
208
-
if err != nil || clientSession.IsNew {
213
-
Handle: clientSession.Values[appview.SessionHandle].(string),
214
-
Did: clientSession.Values[appview.SessionDid].(string),
215
-
Pds: clientSession.Values[appview.SessionPds].(string),