From 07628ed9afe2c8fcfd472ac301ba1937b6233d1e Mon Sep 17 00:00:00 2001 From: Anirudh Oppiliappan Date: Sun, 11 May 2025 10:48:59 +0300 Subject: [PATCH] appview: auth: remove package --- appview/auth/auth.go | 217 ----------------------------------------- appview/state/state.go | 8 -- 2 files changed, 225 deletions(-) delete mode 100644 appview/auth/auth.go diff --git a/appview/auth/auth.go b/appview/auth/auth.go deleted file mode 100644 index a048c39d..00000000 --- a/appview/auth/auth.go +++ /dev/null @@ -1,217 +0,0 @@ -package auth - -import ( - "context" - "fmt" - "net/http" - "time" - - comatproto "github.com/bluesky-social/indigo/api/atproto" - "github.com/bluesky-social/indigo/atproto/identity" - "github.com/bluesky-social/indigo/xrpc" - "github.com/gorilla/sessions" - "tangled.sh/tangled.sh/core/appview" -) - -type Auth struct { - Store *sessions.CookieStore -} - -type AtSessionCreate struct { - comatproto.ServerCreateSession_Output - PDSEndpoint string -} - -type AtSessionRefresh struct { - comatproto.ServerRefreshSession_Output - PDSEndpoint string -} - -func Make(secret string) (*Auth, error) { - store := sessions.NewCookieStore([]byte(secret)) - return &Auth{store}, nil -} - -func (a *Auth) CreateInitialSession(ctx context.Context, resolved *identity.Identity, appPassword string) (*comatproto.ServerCreateSession_Output, error) { - - pdsUrl := resolved.PDSEndpoint() - client := xrpc.Client{ - Host: pdsUrl, - } - - atSession, err := comatproto.ServerCreateSession(ctx, &client, &comatproto.ServerCreateSession_Input{ - Identifier: resolved.DID.String(), - Password: appPassword, - }) - if err != nil { - return nil, fmt.Errorf("invalid app password") - } - - return atSession, nil -} - -// Sessionish is an interface that provides access to the common fields of both types. -type Sessionish interface { - GetAccessJwt() string - GetActive() *bool - GetDid() string - GetDidDoc() *interface{} - GetHandle() string - GetRefreshJwt() string - GetStatus() *string -} - -// Create a wrapper type for ServerRefreshSession_Output -type RefreshSessionWrapper struct { - *comatproto.ServerRefreshSession_Output -} - -func (s *RefreshSessionWrapper) GetAccessJwt() string { - return s.AccessJwt -} - -func (s *RefreshSessionWrapper) GetActive() *bool { - return s.Active -} - -func (s *RefreshSessionWrapper) GetDid() string { - return s.Did -} - -func (s *RefreshSessionWrapper) GetDidDoc() *interface{} { - return s.DidDoc -} - -func (s *RefreshSessionWrapper) GetHandle() string { - return s.Handle -} - -func (s *RefreshSessionWrapper) GetRefreshJwt() string { - return s.RefreshJwt -} - -func (s *RefreshSessionWrapper) GetStatus() *string { - return s.Status -} - -// Create a wrapper type for ServerRefreshSession_Output -type CreateSessionWrapper struct { - *comatproto.ServerCreateSession_Output -} - -func (s *CreateSessionWrapper) GetAccessJwt() string { - return s.AccessJwt -} - -func (s *CreateSessionWrapper) GetActive() *bool { - return s.Active -} - -func (s *CreateSessionWrapper) GetDid() string { - return s.Did -} - -func (s *CreateSessionWrapper) GetDidDoc() *interface{} { - return s.DidDoc -} - -func (s *CreateSessionWrapper) GetHandle() string { - return s.Handle -} - -func (s *CreateSessionWrapper) GetRefreshJwt() string { - return s.RefreshJwt -} - -func (s *CreateSessionWrapper) GetStatus() *string { - return s.Status -} - -func (a *Auth) ClearSession(r *http.Request, w http.ResponseWriter) error { - clientSession, err := a.Store.Get(r, appview.SessionName) - if err != nil { - return fmt.Errorf("invalid session", err) - } - if clientSession.IsNew { - return fmt.Errorf("invalid session") - } - clientSession.Options.MaxAge = -1 - return clientSession.Save(r, w) -} - -func (a *Auth) StoreSession(r *http.Request, w http.ResponseWriter, atSessionish Sessionish, pdsEndpoint string) error { - clientSession, _ := a.Store.Get(r, appview.SessionName) - clientSession.Values[appview.SessionHandle] = atSessionish.GetHandle() - clientSession.Values[appview.SessionDid] = atSessionish.GetDid() - clientSession.Values[appview.SessionPds] = pdsEndpoint - clientSession.Values[appview.SessionAccessJwt] = atSessionish.GetAccessJwt() - clientSession.Values[appview.SessionRefreshJwt] = atSessionish.GetRefreshJwt() - clientSession.Values[appview.SessionExpiry] = time.Now().Add(time.Minute * 15).Format(time.RFC3339) - clientSession.Values[appview.SessionAuthenticated] = true - return clientSession.Save(r, w) -} - -func (a *Auth) AuthorizedClient(r *http.Request) (*xrpc.Client, error) { - clientSession, err := a.Store.Get(r, "appview-session") - if err != nil || clientSession.IsNew { - return nil, err - } - - did := clientSession.Values["did"].(string) - pdsUrl := clientSession.Values["pds"].(string) - accessJwt := clientSession.Values["accessJwt"].(string) - refreshJwt := clientSession.Values["refreshJwt"].(string) - - client := &xrpc.Client{ - Host: pdsUrl, - Auth: &xrpc.AuthInfo{ - AccessJwt: accessJwt, - RefreshJwt: refreshJwt, - Did: did, - }, - } - - return client, nil -} - -func (a *Auth) GetSession(r *http.Request) (*sessions.Session, error) { - return a.Store.Get(r, appview.SessionName) -} - -func (a *Auth) GetDid(r *http.Request) string { - clientSession, err := a.Store.Get(r, appview.SessionName) - if err != nil || clientSession.IsNew { - return "" - } - - return clientSession.Values[appview.SessionDid].(string) -} - -func (a *Auth) GetHandle(r *http.Request) string { - clientSession, err := a.Store.Get(r, appview.SessionName) - if err != nil || clientSession.IsNew { - return "" - } - - return clientSession.Values[appview.SessionHandle].(string) -} - -type User struct { - Handle string - Did string - Pds string -} - -func (a *Auth) GetUser(r *http.Request) *User { - clientSession, err := a.Store.Get(r, appview.SessionName) - - if err != nil || clientSession.IsNew { - return nil - } - - return &User{ - Handle: clientSession.Values[appview.SessionHandle].(string), - Did: clientSession.Values[appview.SessionDid].(string), - Pds: clientSession.Values[appview.SessionPds].(string), - } -} diff --git a/appview/state/state.go b/appview/state/state.go index 7bb64d7d..aa43e62d 100644 --- a/appview/state/state.go +++ b/appview/state/state.go @@ -19,7 +19,6 @@ import ( "github.com/go-chi/chi/v5" "tangled.sh/tangled.sh/core/api/tangled" "tangled.sh/tangled.sh/core/appview" - "tangled.sh/tangled.sh/core/appview/auth" "tangled.sh/tangled.sh/core/appview/db" "tangled.sh/tangled.sh/core/appview/oauth" "tangled.sh/tangled.sh/core/appview/pages" @@ -29,7 +28,6 @@ import ( type State struct { db *db.DB - auth *auth.Auth oauth *oauth.OAuth enforcer *rbac.Enforcer tidClock syntax.TIDClock @@ -45,11 +43,6 @@ func Make(config *appview.Config) (*State, error) { return nil, err } - auth, err := auth.Make(config.Core.CookieSecret) - if err != nil { - return nil, err - } - enforcer, err := rbac.NewEnforcer(config.Core.DbPath) if err != nil { return nil, err @@ -89,7 +82,6 @@ func Make(config *appview.Config) (*State, error) { state := &State{ d, - auth, oauth, enforcer, clock, -- 2.51.2