···
"github.com/gorilla/sessions"
oauth "tangled.sh/icyphox.sh/atproto-oauth"
"tangled.sh/icyphox.sh/atproto-oauth/helpers"
13
+
sessioncache "tangled.sh/tangled.sh/core/appview/cache/session"
"tangled.sh/tangled.sh/core/appview/config"
14
-
"tangled.sh/tangled.sh/core/appview/db"
"tangled.sh/tangled.sh/core/appview/oauth/client"
xrpc "tangled.sh/tangled.sh/core/appview/xrpcclient"
19
-
type OAuthRequest struct {
21
-
AuthserverIss string
26
-
DpopAuthserverNonce string
27
-
DpopPrivateJwk string
31
-
Store *sessions.CookieStore
33
-
Config *config.Config
20
+
store *sessions.CookieStore
21
+
config *config.Config
22
+
sess *sessioncache.SessionStore
36
-
func NewOAuth(db *db.DB, config *config.Config) *OAuth {
25
+
func NewOAuth(config *config.Config, sess *sessioncache.SessionStore) *OAuth {
38
-
Store: sessions.NewCookieStore([]byte(config.Core.CookieSecret)),
27
+
store: sessions.NewCookieStore([]byte(config.Core.CookieSecret)),
44
-
func (o *OAuth) SaveSession(w http.ResponseWriter, r *http.Request, oreq db.OAuthRequest, oresp *oauth.TokenResponse) error {
33
+
func (o *OAuth) Stores() *sessions.CookieStore {
37
+
func (o *OAuth) SaveSession(w http.ResponseWriter, r *http.Request, oreq sessioncache.OAuthRequest, oresp *oauth.TokenResponse) error {
// first we save the did in the user session
46
-
userSession, err := o.Store.Get(r, SessionName)
39
+
userSession, err := o.store.Get(r, SessionName)
···
// then save the whole thing in the db
61
-
session := db.OAuthSession{
54
+
session := sessioncache.OAuthSession{
···
Expiry: time.Now().Add(time.Duration(oresp.ExpiresIn) * time.Second).Format(time.RFC3339),
73
-
return db.SaveOAuthSession(o.Db, session)
66
+
return o.sess.SaveSession(r.Context(), session)
func (o *OAuth) ClearSession(r *http.Request, w http.ResponseWriter) error {
77
-
userSession, err := o.Store.Get(r, SessionName)
70
+
userSession, err := o.store.Get(r, SessionName)
if err != nil || userSession.IsNew {
return fmt.Errorf("error getting user session (or new session?): %w", err)
did := userSession.Values[SessionDid].(string)
84
-
err = db.DeleteOAuthSessionByDid(o.Db, did)
77
+
err = o.sess.DeleteSession(r.Context(), did)
return fmt.Errorf("error deleting oauth session: %w", err)
···
return userSession.Save(r, w)
94
-
func (o *OAuth) GetSession(r *http.Request) (*db.OAuthSession, bool, error) {
95
-
userSession, err := o.Store.Get(r, SessionName)
87
+
func (o *OAuth) GetSession(r *http.Request) (*sessioncache.OAuthSession, bool, error) {
88
+
userSession, err := o.store.Get(r, SessionName)
if err != nil || userSession.IsNew {
return nil, false, fmt.Errorf("error getting user session (or new session?): %w", err)
···
did := userSession.Values[SessionDid].(string)
auth := userSession.Values[SessionAuthenticated].(bool)
103
-
session, err := db.GetOAuthSessionByDid(o.Db, did)
96
+
session, err := o.sess.GetSession(r.Context(), did)
return nil, false, fmt.Errorf("error getting oauth session: %w", err)
···
oauthClient, err := client.NewClient(
122
-
o.Config.OAuth.Jwks,
115
+
o.config.OAuth.Jwks,
···
newExpiry := time.Now().Add(time.Duration(resp.ExpiresIn) * time.Second).Format(time.RFC3339)
136
-
err = db.RefreshOAuthSession(o.Db, did, resp.AccessToken, resp.RefreshToken, newExpiry)
129
+
err = o.sess.RefreshSession(r.Context(), did, resp.AccessToken, resp.RefreshToken, newExpiry)
return nil, false, fmt.Errorf("error refreshing oauth session: %w", err)
···
func (a *OAuth) GetUser(r *http.Request) *User {
158
-
clientSession, err := a.Store.Get(r, SessionName)
151
+
clientSession, err := a.store.Get(r, SessionName)
if err != nil || clientSession.IsNew {
···
func (a *OAuth) GetDid(r *http.Request) string {
172
-
clientSession, err := a.Store.Get(r, SessionName)
165
+
clientSession, err := a.store.Get(r, SessionName)
if err != nil || clientSession.IsNew {
···
client := &oauth.XrpcClient{
OnDpopPdsNonceChanged: func(did, newNonce string) {
192
-
err := db.UpdateDpopPdsNonce(o.Db, did, newNonce)
185
+
err := o.sess.UpdateNonce(r.Context(), did, newNonce)
log.Printf("error updating dpop pds nonce: %v", err)
···
return []string{fmt.Sprintf("%s/oauth/callback", c)}
237
-
clientURI := o.Config.Core.AppviewHost
230
+
clientURI := o.config.Core.AppviewHost
clientID := fmt.Sprintf("%s/oauth/client-metadata.json", clientURI)
redirectURIs := makeRedirectURIs(clientURI)
241
-
if o.Config.Core.Dev {
234
+
if o.config.Core.Dev {
clientURI = fmt.Sprintf("http://127.0.0.1:3000")
redirectURIs = makeRedirectURIs(clientURI)