feat: initial latha-auth CLI for Gitea PKCE relay and Zitadel Device Code
This commit is contained in:
commit
82d6862a0e
9 files changed
+682
No files matched your search
@@ -0,0 +1,103 @@
|
||||
package gitea
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestBuildAuthorizeURL(t *testing.T) {
|
||||
uStr := BuildAuthorizeURL(
|
||||
"https://git.site.latha.org",
|
||||
"cid-123",
|
||||
"https://deploy.site.latha.org/oauth/callback",
|
||||
"st-456",
|
||||
"chal-789",
|
||||
)
|
||||
u, err := url.Parse(uStr)
|
||||
if err != nil {
|
||||
t.Fatalf("parse url: %v", err)
|
||||
}
|
||||
if u.Path != "/login/oauth/authorize" {
|
||||
t.Fatalf("unexpected path: %s", u.Path)
|
||||
}
|
||||
q := u.Query()
|
||||
if q.Get("client_id") != "cid-123" ||
|
||||
q.Get("redirect_uri") != "https://deploy.site.latha.org/oauth/callback" ||
|
||||
q.Get("response_type") != "code" ||
|
||||
q.Get("state") != "st-456" ||
|
||||
q.Get("code_challenge") != "chal-789" ||
|
||||
q.Get("code_challenge_method") != "S256" {
|
||||
t.Fatalf("unexpected query params: %v", q)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPollRelayAndExchangeCode(t *testing.T) {
|
||||
var polls atomic.Int32
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/api/oauth/poll":
|
||||
if r.URL.Query().Get("state") != "st-1" {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
n := polls.Add(1)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if n < 2 {
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
_ = json.NewEncoder(w).Encode(map[string]string{"status": "pending"})
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_ = json.NewEncoder(w).Encode(map[string]string{"status": "ready", "code": "code-abc"})
|
||||
|
||||
case "/login/oauth/access_token":
|
||||
if err := r.ParseForm(); err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if r.Form.Get("grant_type") != "authorization_code" ||
|
||||
r.Form.Get("client_id") != "cid-1" ||
|
||||
r.Form.Get("code") != "code-abc" ||
|
||||
r.Form.Get("code_verifier") != "ver-xyz" {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
_ = json.NewEncoder(w).Encode(map[string]string{"error": "invalid_request"})
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(TokenResponse{
|
||||
AccessToken: "tok-999",
|
||||
TokenType: "bearer",
|
||||
ExpiresIn: 3600,
|
||||
})
|
||||
|
||||
default:
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
code, err := PollRelay(ctx, srv.Client(), srv.URL, "st-1", 10*time.Millisecond)
|
||||
if err != nil {
|
||||
t.Fatalf("PollRelay: %v", err)
|
||||
}
|
||||
if code != "code-abc" {
|
||||
t.Fatalf("code = %q, want %q", code, "code-abc")
|
||||
}
|
||||
|
||||
tok, err := ExchangeCode(ctx, srv.Client(), srv.URL, "cid-1", RedirectURI(srv.URL), code, "ver-xyz")
|
||||
if err != nil {
|
||||
t.Fatalf("ExchangeCode: %v", err)
|
||||
}
|
||||
if tok.AccessToken != "tok-999" {
|
||||
t.Fatalf("AccessToken = %q, want %q", tok.AccessToken, "tok-999")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user