Discover books, shows, and movies at your level. Track your progress by filling your Shelf with what you find, and share with other language learners. *No dusting required. shlf.space
at master 76 lines 1.9 kB view raw
1package oauth 2 3import ( 4 "encoding/json" 5 "errors" 6 "fmt" 7 "net/http" 8 9 "github.com/bluesky-social/indigo/atproto/auth/oauth" 10 "github.com/go-chi/chi/v5" 11) 12 13func (o *OAuth) Router() http.Handler { 14 r := chi.NewRouter() 15 16 r.Get("/oauth/client-metadata.json", o.clientMetadata) 17 r.Get("/oauth/jwks.json", o.jwks) 18 r.Get("/oauth/callback", o.callback) 19 20 return r 21} 22 23func (o *OAuth) clientMetadata(w http.ResponseWriter, r *http.Request) { 24 clientName := ClientName 25 clientUri := ClientURI 26 27 meta := o.ClientApp.Config.ClientMetadata() 28 meta.JWKSURI = &o.JwksUri 29 meta.ClientName = &clientName 30 meta.ClientURI = &clientUri 31 32 w.Header().Set("Content-Type", "application/json") 33 if err := json.NewEncoder(w).Encode(meta); err != nil { 34 http.Error(w, err.Error(), http.StatusInternalServerError) 35 return 36 } 37} 38 39func (o *OAuth) jwks(w http.ResponseWriter, r *http.Request) { 40 w.Header().Set("Content-Type", "application/json") 41 body := o.ClientApp.Config.PublicJWKS() 42 if err := json.NewEncoder(w).Encode(body); err != nil { 43 http.Error(w, err.Error(), http.StatusInternalServerError) 44 return 45 } 46} 47 48func (o *OAuth) callback(w http.ResponseWriter, r *http.Request) { 49 ctx := r.Context() 50 51 authReturn := o.GetAuthReturn(r) 52 _ = o.ClearAuthReturn(w, r) 53 54 sessData, err := o.ClientApp.ProcessCallback(ctx, r.URL.Query()) 55 if err != nil { 56 var callbackErr *oauth.AuthRequestCallbackError 57 if errors.As(err, &callbackErr) { 58 http.Redirect(w, r, fmt.Sprintf("/login?error=%s", callbackErr.ErrorCode), http.StatusFound) 59 return 60 } 61 http.Redirect(w, r, "/login?error=oauth", http.StatusFound) 62 return 63 } 64 65 if err := o.SaveSession(w, r, sessData); err != nil { 66 http.Redirect(w, r, "/login?error=session", http.StatusFound) 67 return 68 } 69 70 redirectURL := "/" 71 if authReturn.ReturnURL != "" { 72 redirectURL = authReturn.ReturnURL 73 } 74 75 http.Redirect(w, r, redirectURL, http.StatusFound) 76}