Build and Push Image / build (push) Successful in 40s
Add GET /oauth/atproto-client-metadata.json next to the existing OAuth relay routes so AT Protocol authorization servers can resolve the latha-auth public client (client_id is this URL).
142 lines
4.5 KiB
Go
142 lines
4.5 KiB
Go
package server
|
|
|
|
import (
|
|
"html"
|
|
"net/http"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
const oauthRelayTTL = 5 * time.Minute
|
|
|
|
type oauthRelayEntry struct {
|
|
code string
|
|
expiresAt time.Time
|
|
}
|
|
|
|
type oauthRelayStore struct {
|
|
mu sync.Mutex
|
|
entries map[string]oauthRelayEntry
|
|
ttl time.Duration
|
|
now func() time.Time
|
|
}
|
|
|
|
func newOAuthRelayStore() *oauthRelayStore {
|
|
return &oauthRelayStore{
|
|
entries: make(map[string]oauthRelayEntry),
|
|
ttl: oauthRelayTTL,
|
|
now: time.Now,
|
|
}
|
|
}
|
|
|
|
func (o *oauthRelayStore) Set(state, code string) {
|
|
o.mu.Lock()
|
|
defer o.mu.Unlock()
|
|
now := o.now()
|
|
o.pruneLocked(now)
|
|
o.entries[state] = oauthRelayEntry{
|
|
code: code,
|
|
expiresAt: now.Add(o.ttl),
|
|
}
|
|
}
|
|
|
|
func (o *oauthRelayStore) Take(state string) (string, bool) {
|
|
o.mu.Lock()
|
|
defer o.mu.Unlock()
|
|
now := o.now()
|
|
o.pruneLocked(now)
|
|
entry, ok := o.entries[state]
|
|
if !ok {
|
|
return "", false
|
|
}
|
|
delete(o.entries, state)
|
|
if now.After(entry.expiresAt) {
|
|
return "", false
|
|
}
|
|
return entry.code, true
|
|
}
|
|
|
|
func (o *oauthRelayStore) pruneLocked(now time.Time) {
|
|
for k, v := range o.entries {
|
|
if now.After(v.expiresAt) {
|
|
delete(o.entries, k)
|
|
}
|
|
}
|
|
}
|
|
|
|
// atprotoClientMetadata is the OAuth Client ID Metadata Document for
|
|
// the latha-auth AT Protocol public client. It must stay available at
|
|
// exactly the client_id URL for the life of any session issued against
|
|
// it. Source of truth: `latha-auth atproto --print-client-metadata`.
|
|
var atprotoClientMetadata = struct {
|
|
ClientID string `json:"client_id"`
|
|
ClientName string `json:"client_name"`
|
|
GrantTypes []string `json:"grant_types"`
|
|
ResponseTypes []string `json:"response_types"`
|
|
RedirectURIs []string `json:"redirect_uris"`
|
|
Scope string `json:"scope"`
|
|
TokenEndpointAuthMethod string `json:"token_endpoint_auth_method"`
|
|
DPoPBoundAccessTokens bool `json:"dpop_bound_access_tokens"`
|
|
}{
|
|
ClientID: "https://deploy.site.latha.org/oauth/atproto-client-metadata.json",
|
|
ClientName: "latha-auth",
|
|
GrantTypes: []string{"authorization_code", "refresh_token"},
|
|
ResponseTypes: []string{"code"},
|
|
RedirectURIs: []string{"https://deploy.site.latha.org/oauth/callback"},
|
|
Scope: "atproto transition:generic",
|
|
TokenEndpointAuthMethod: "none",
|
|
DPoPBoundAccessTokens: true,
|
|
}
|
|
|
|
func (s *Server) handleAtprotoClientMetadata(w http.ResponseWriter, r *http.Request) {
|
|
writeJSON(w, http.StatusOK, atprotoClientMetadata)
|
|
}
|
|
|
|
func (s *Server) handleOAuthCallback(w http.ResponseWriter, r *http.Request) {
|
|
q := r.URL.Query()
|
|
if errParam := q.Get("error"); errParam != "" {
|
|
desc := q.Get("error_description")
|
|
if desc == "" {
|
|
desc = errParam
|
|
}
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
_, _ = w.Write([]byte(`<!doctype html><html><head><meta charset="utf-8"><title>Authorization Failed</title></head><body style="font-family:system-ui,sans-serif;max-width:480px;margin:4rem auto;padding:0 1rem;text-align:center;"><h1>Authorization Failed</h1><p>` + html.EscapeString(desc) + `</p></body></html>`))
|
|
return
|
|
}
|
|
|
|
state := q.Get("state")
|
|
code := q.Get("code")
|
|
if state == "" || code == "" {
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
_, _ = w.Write([]byte(`<!doctype html><html><head><meta charset="utf-8"><title>Invalid OAuth Callback</title></head><body style="font-family:system-ui,sans-serif;max-width:480px;margin:4rem auto;padding:0 1rem;text-align:center;"><h1>Invalid OAuth Callback</h1><p>Missing required <code>code</code> or <code>state</code> query parameter.</p></body></html>`))
|
|
return
|
|
}
|
|
|
|
s.oauth.Set(state, code)
|
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
w.WriteHeader(http.StatusOK)
|
|
_, _ = w.Write([]byte(`<!doctype html><html><head><meta charset="utf-8"><title>Authorized</title></head><body style="font-family:system-ui,sans-serif;max-width:480px;margin:4rem auto;padding:0 1rem;text-align:center;"><h1>Authorized!</h1><p>Authorized! You can close this tab.</p></body></html>`))
|
|
}
|
|
|
|
func (s *Server) handleOAuthPoll(w http.ResponseWriter, r *http.Request) {
|
|
state := r.URL.Query().Get("state")
|
|
if state == "" {
|
|
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "state is required"})
|
|
return
|
|
}
|
|
|
|
code, ok := s.oauth.Take(state)
|
|
if !ok {
|
|
writeJSON(w, http.StatusAccepted, map[string]string{"status": "pending"})
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, map[string]string{
|
|
"status": "ready",
|
|
"code": code,
|
|
})
|
|
}
|