package auth import ( "context" "encoding/json" "fmt" "net/http" "os" "github.com/bluesky-social/indigo/atproto/auth/oauth" ) var oAuthApp *oauth.ClientApp = nil func OauthCallback(authChannel chan *oauth.ClientSession) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { ctx := r.Context() sessData, err := oAuthApp.ProcessCallback(ctx, r.URL.Query()) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) authChannel <- nil } sess, err := oAuthApp.ResumeSession(context.Background(), sessData.AccountDID, sessData.SessionID) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) authChannel <- nil } authChannel <- sess } } func ClientMetadata(w http.ResponseWriter, r *http.Request) { doc := oAuthApp.Config.ClientMetadata() w.Header().Set("Content-Type", "application/json") if err := json.NewEncoder(w).Encode(doc); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } func askAuth(oauthApp *oauth.ClientApp) { flow, err := oauthApp.StartAuthFlow(context.Background(), os.Getenv("HANDLE")) if err != nil { panic(err) } fmt.Printf("Please connect to your PDS using this link : %s\n", flow) } func StartServer(authChannel chan *oauth.ClientSession) { config := oauth.NewLocalhostConfig("http://127.0.0.1:3000/oauth/callback", []string{"atproto", "repo:fm.teal.alpha.feed.play"}) oAuthApp = oauth.NewClientApp(&config, oauth.NewMemStore()) http.HandleFunc("GET /client-metadata.json", ClientMetadata) http.HandleFunc("/oauth/callback", OauthCallback(authChannel)) askAuth(oAuthApp) http.ListenAndServe("127.0.0.1:3000", http.DefaultServeMux) }