A way to send current playing track in cider to teal collection
1package auth
2
3import (
4 "context"
5 "encoding/json"
6 "fmt"
7 "net/http"
8 "os"
9
10 "github.com/bluesky-social/indigo/atproto/auth/oauth"
11)
12
13var oAuthApp *oauth.ClientApp = nil
14
15func OauthCallback(authChannel chan *oauth.ClientSession) http.HandlerFunc {
16 return func(w http.ResponseWriter, r *http.Request) {
17 ctx := r.Context()
18 sessData, err := oAuthApp.ProcessCallback(ctx, r.URL.Query())
19 if err != nil {
20 http.Error(w, err.Error(), http.StatusInternalServerError)
21 authChannel <- nil
22 }
23
24 sess, err := oAuthApp.ResumeSession(context.Background(), sessData.AccountDID, sessData.SessionID)
25 if err != nil {
26 http.Error(w, err.Error(), http.StatusInternalServerError)
27 authChannel <- nil
28 }
29
30 authChannel <- sess
31 }
32}
33
34func ClientMetadata(w http.ResponseWriter, r *http.Request) {
35 doc := oAuthApp.Config.ClientMetadata()
36 w.Header().Set("Content-Type", "application/json")
37 if err := json.NewEncoder(w).Encode(doc); err != nil {
38 http.Error(w, err.Error(), http.StatusInternalServerError)
39 }
40}
41
42func askAuth(oauthApp *oauth.ClientApp) {
43 flow, err := oauthApp.StartAuthFlow(context.Background(), os.Getenv("HANDLE"))
44 if err != nil {
45 panic(err)
46 }
47 fmt.Printf("Please connect to your PDS using this link : %s\n", flow)
48}
49
50func StartServer(authChannel chan *oauth.ClientSession) {
51 config := oauth.NewLocalhostConfig("http://127.0.0.1:3000/oauth/callback", []string{"atproto", "repo:fm.teal.alpha.feed.play"})
52 oAuthApp = oauth.NewClientApp(&config, oauth.NewMemStore())
53
54 http.HandleFunc("GET /client-metadata.json", ClientMetadata)
55 http.HandleFunc("/oauth/callback", OauthCallback(authChannel))
56
57 askAuth(oAuthApp)
58 http.ListenAndServe("127.0.0.1:3000", http.DefaultServeMux)
59}