Working on Posting

This commit is contained in:
2026-02-04 22:06:36 -06:00
parent 4ed5d821da
commit 687e64e701
4 changed files with 79 additions and 14 deletions

View File

@@ -21,12 +21,16 @@ type AuthRepo struct {
oauthClient *oauth.ClientApp
oauthConfig *oauth.ClientConfig
store *SqliteStore
context context.Context
session *oauth.ClientSessionData
}
func NewAuthRepo(r *Repo) (*AuthRepo, error) {
a := &AuthRepo{r: r}
a := &AuthRepo{
r: r,
context: r.context,
}
var err error
a.oauthConfig, a.oauthClient, a.store, err = a.buildOAuthClient()
if err != nil {
@@ -39,7 +43,7 @@ func NewAuthRepo(r *Repo) (*AuthRepo, error) {
func (r *AuthRepo) buildOAuthClient() (*oauth.ClientConfig, *oauth.ClientApp, *SqliteStore, error) {
config := oauth.ClientConfig{
ClientID: "https://expds.bullercodeworks.com/oauth-client-metadata.json",
Scopes: []string{"atproto", "repo:*"},
Scopes: []string{"atproto", "repo:*", "blob:*/*"},
UserAgent: "expds",
}
@@ -57,9 +61,9 @@ func (r *AuthRepo) buildOAuthClient() (*oauth.ClientConfig, *oauth.ClientApp, *S
return &config, oauthClient, store, nil
}
func (r *AuthRepo) StartAuthFlow(port int, ctx context.Context, identifier string, callbackRes chan url.Values) (string, error) {
func (r *AuthRepo) StartAuthFlow(port int, identifier string, callbackRes chan url.Values) (string, error) {
r.oauthConfig.CallbackURL = fmt.Sprintf("http://127.0.0.1:%d/callback", port)
authUrl, err := r.oauthClient.StartAuthFlow(ctx, identifier)
authUrl, err := r.oauthClient.StartAuthFlow(r.context, identifier)
if err != nil {
return "", fmt.Errorf("error logging in: %w", err)
}
@@ -68,7 +72,7 @@ func (r *AuthRepo) StartAuthFlow(port int, ctx context.Context, identifier strin
}
exec.Command("xdg-open", authUrl).Run()
r.session, err = r.oauthClient.ProcessCallback(ctx, <-callbackRes)
r.session, err = r.oauthClient.ProcessCallback(r.context, <-callbackRes)
if err != nil {
return "", err
}
@@ -82,7 +86,7 @@ func (r *AuthRepo) prepareDbPath() string {
}
// HTTP Server listening for OAuth Response
func (r *AuthRepo) ListenForCallback(ctx context.Context, res chan url.Values) (int, error) {
func (r *AuthRepo) ListenForCallback(res chan url.Values) (int, error) {
listener, err := net.Listen("tcp", ":0")
if err != nil {
return 0, err
@@ -93,12 +97,12 @@ func (r *AuthRepo) ListenForCallback(ctx context.Context, res chan url.Values) (
Handler: mux,
}
mux.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) {
res <- r.URL.Query()
mux.HandleFunc("/callback", func(w http.ResponseWriter, req *http.Request) {
res <- req.URL.Query()
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(200)
w.Write([]byte("<!DOCTYPE html><html><body><h2>expds</h2><p>You can safely close this window and return to your application.</p></body></html>\n"))
go server.Shutdown(ctx)
go server.Shutdown(r.context)
})
go func() {
@@ -110,3 +114,16 @@ func (r *AuthRepo) ListenForCallback(ctx context.Context, res chan url.Values) (
return listener.Addr().(*net.TCPAddr).Port, nil
}
func (r *AuthRepo) HasAuth() bool {
sess, err := r.store.GetMostRecentSession(r.context)
return err != nil || sess == nil
}
func (r *AuthRepo) GetSession() (*oauth.ClientSession, error) {
sess, err := r.store.GetMostRecentSession(r.context)
if err != nil {
return nil, fmt.Errorf("error getting most recent session: %w", err)
}
return r.oauthClient.ResumeSession(r.context, sess.AccountDID, sess.SessionID)
}