···
"github.com/gorilla/sessions"
oauth "tangled.sh/icyphox.sh/atproto-oauth"
"tangled.sh/icyphox.sh/atproto-oauth/helpers"
"tangled.sh/tangled.sh/core/appview/config"
-
"tangled.sh/tangled.sh/core/appview/db"
"tangled.sh/tangled.sh/core/appview/oauth/client"
xrpc "tangled.sh/tangled.sh/core/appview/xrpcclient"
-
type OAuthRequest struct {
-
DpopAuthserverNonce string
-
Store *sessions.CookieStore
-
func NewOAuth(db *db.DB, config *config.Config) *OAuth {
-
Store: sessions.NewCookieStore([]byte(config.Core.CookieSecret)),
-
func (o *OAuth) SaveSession(w http.ResponseWriter, r *http.Request, oreq db.OAuthRequest, oresp *oauth.TokenResponse) error {
// first we save the did in the user session
-
userSession, err := o.Store.Get(r, SessionName)
···
// then save the whole thing in the db
-
session := db.OAuthSession{
···
Expiry: time.Now().Add(time.Duration(oresp.ExpiresIn) * time.Second).Format(time.RFC3339),
-
return db.SaveOAuthSession(o.Db, session)
func (o *OAuth) ClearSession(r *http.Request, w http.ResponseWriter) error {
-
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)
-
err = db.DeleteOAuthSessionByDid(o.Db, did)
return fmt.Errorf("error deleting oauth session: %w", err)
···
return userSession.Save(r, w)
-
func (o *OAuth) GetSession(r *http.Request) (*db.OAuthSession, bool, error) {
-
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)
-
session, err := db.GetOAuthSessionByDid(o.Db, did)
return nil, false, fmt.Errorf("error getting oauth session: %w", err)
···
oauthClient, err := client.NewClient(
···
newExpiry := time.Now().Add(time.Duration(resp.ExpiresIn) * time.Second).Format(time.RFC3339)
-
err = db.RefreshOAuthSession(o.Db, 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 {
-
clientSession, err := a.Store.Get(r, SessionName)
if err != nil || clientSession.IsNew {
···
func (a *OAuth) GetDid(r *http.Request) string {
-
clientSession, err := a.Store.Get(r, SessionName)
if err != nil || clientSession.IsNew {
···
client := &oauth.XrpcClient{
OnDpopPdsNonceChanged: func(did, newNonce string) {
-
err := db.UpdateDpopPdsNonce(o.Db, did, newNonce)
log.Printf("error updating dpop pds nonce: %v", err)
···
return []string{fmt.Sprintf("%s/oauth/callback", c)}
-
clientURI := o.Config.Core.AppviewHost
clientID := fmt.Sprintf("%s/oauth/client-metadata.json", clientURI)
redirectURIs := makeRedirectURIs(clientURI)
clientURI = fmt.Sprintf("http://127.0.0.1:3000")
redirectURIs = makeRedirectURIs(clientURI)
···
"github.com/gorilla/sessions"
oauth "tangled.sh/icyphox.sh/atproto-oauth"
"tangled.sh/icyphox.sh/atproto-oauth/helpers"
+
sessioncache "tangled.sh/tangled.sh/core/appview/cache/session"
"tangled.sh/tangled.sh/core/appview/config"
"tangled.sh/tangled.sh/core/appview/oauth/client"
xrpc "tangled.sh/tangled.sh/core/appview/xrpcclient"
+
store *sessions.CookieStore
+
sess *sessioncache.SessionStore
+
func NewOAuth(config *config.Config, sess *sessioncache.SessionStore) *OAuth {
+
store: sessions.NewCookieStore([]byte(config.Core.CookieSecret)),
+
func (o *OAuth) Stores() *sessions.CookieStore {
+
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
+
userSession, err := o.store.Get(r, SessionName)
···
// then save the whole thing in the db
+
session := sessioncache.OAuthSession{
···
Expiry: time.Now().Add(time.Duration(oresp.ExpiresIn) * time.Second).Format(time.RFC3339),
+
return o.sess.SaveSession(r.Context(), session)
func (o *OAuth) ClearSession(r *http.Request, w http.ResponseWriter) error {
+
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)
+
err = o.sess.DeleteSession(r.Context(), did)
return fmt.Errorf("error deleting oauth session: %w", err)
···
return userSession.Save(r, w)
+
func (o *OAuth) GetSession(r *http.Request) (*sessioncache.OAuthSession, bool, error) {
+
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)
+
session, err := o.sess.GetSession(r.Context(), did)
return nil, false, fmt.Errorf("error getting oauth session: %w", err)
···
oauthClient, err := client.NewClient(
···
newExpiry := time.Now().Add(time.Duration(resp.ExpiresIn) * time.Second).Format(time.RFC3339)
+
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 {
+
clientSession, err := a.store.Get(r, SessionName)
if err != nil || clientSession.IsNew {
···
func (a *OAuth) GetDid(r *http.Request) string {
+
clientSession, err := a.store.Get(r, SessionName)
if err != nil || clientSession.IsNew {
···
client := &oauth.XrpcClient{
OnDpopPdsNonceChanged: func(did, newNonce string) {
+
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)}
+
clientURI := o.config.Core.AppviewHost
clientID := fmt.Sprintf("%s/oauth/client-metadata.json", clientURI)
redirectURIs := makeRedirectURIs(clientURI)
clientURI = fmt.Sprintf("http://127.0.0.1:3000")
redirectURIs = makeRedirectURIs(clientURI)