package indieauth import ( "encoding/json" "fmt" "io" "net/http" "github.com/owncast/owncast/auth" ia "github.com/owncast/owncast/auth/indieauth" "github.com/owncast/owncast/controllers" "github.com/owncast/owncast/core/chat" "github.com/owncast/owncast/core/user" log "github.com/sirupsen/logrus" ) // StartAuthFlow will begin the IndieAuth flow for the current user. func StartAuthFlow(u user.User, w http.ResponseWriter, r *http.Request) { type request struct { AuthHost string `json:"authHost"` } type response struct { Redirect string `json:"redirect"` } var authRequest request p, err := io.ReadAll(r.Body) if err != nil { controllers.WriteSimpleResponse(w, false, err.Error()) return } if err := json.Unmarshal(p, &authRequest); err != nil { controllers.WriteSimpleResponse(w, false, err.Error()) return } accessToken := r.URL.Query().Get("accessToken") redirectURL, err := ia.StartAuthFlow(authRequest.AuthHost, u.ID, accessToken, u.DisplayName) if err != nil { controllers.WriteSimpleResponse(w, false, err.Error()) return } redirectResponse := response{ Redirect: redirectURL.String(), } controllers.WriteResponse(w, redirectResponse) } // HandleRedirect will handle the redirect from an IndieAuth server to // continue the auth flow. func HandleRedirect(w http.ResponseWriter, r *http.Request) { state := r.URL.Query().Get("state") code := r.URL.Query().Get("code") request, response, err := ia.HandleCallbackCode(code, state) if err != nil { log.Debugln(err) msg := `Unable to complete authentication. Go back.