Merge pull request 'feat: sign up and sign in with platform identity providers' (#123) from feat/platform-sso into main
This commit was merged in pull request #123.
This commit is contained in:
commit
29b9e1ce2b
26 files changed
+2710
-351
No files matched your search
@@ -0,0 +1,93 @@
|
||||
package sso
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
// GitHub signs users in with OAuth instead of OpenID Connect. A Provider
|
||||
// with Kind GitHub names the GitHub server as Issuer: https://github.com or
|
||||
// a GitHub Enterprise host.
|
||||
const GitHub = "github"
|
||||
|
||||
func (c *Client) githubOAuth(p Provider) *oauth2.Config {
|
||||
web := strings.TrimSuffix(p.Issuer, "/")
|
||||
return &oauth2.Config{
|
||||
ClientID: p.ClientID,
|
||||
ClientSecret: p.ClientSecret,
|
||||
Endpoint: oauth2.Endpoint{
|
||||
AuthURL: web + "/login/oauth/authorize", TokenURL: web + "/login/oauth/access_token",
|
||||
AuthStyle: oauth2.AuthStyleInParams,
|
||||
},
|
||||
RedirectURL: c.redirectURL,
|
||||
Scopes: []string{"read:user", "user:email"},
|
||||
}
|
||||
}
|
||||
|
||||
// githubAPI is the REST API of a GitHub server.
|
||||
func githubAPI(issuer string) string {
|
||||
web := strings.TrimSuffix(issuer, "/")
|
||||
if web == "https://github.com" {
|
||||
return "https://api.github.com"
|
||||
}
|
||||
return web + "/api/v3"
|
||||
}
|
||||
|
||||
// githubClaims reads the user behind tok. Only the primary email counts,
|
||||
// and only if GitHub verified it.
|
||||
func (c *Client) githubClaims(ctx context.Context, p Provider, tok *oauth2.Token) (Claims, error) {
|
||||
get := func(path string, v any) error {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, githubAPI(p.Issuer)+path, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+tok.AccessToken)
|
||||
req.Header.Set("Accept", "application/vnd.github+json")
|
||||
resp, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("sso: github %s answered %d", path, resp.StatusCode)
|
||||
}
|
||||
return json.NewDecoder(io.LimitReader(resp.Body, 1<<20)).Decode(v)
|
||||
}
|
||||
var user struct {
|
||||
ID int64 `json:"id"`
|
||||
Login string `json:"login"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
if err := get("/user", &user); err != nil {
|
||||
return Claims{}, err
|
||||
}
|
||||
if user.ID == 0 {
|
||||
return Claims{}, errors.New("sso: github user without id")
|
||||
}
|
||||
var emails []struct {
|
||||
Email string `json:"email"`
|
||||
Primary bool `json:"primary"`
|
||||
Verified bool `json:"verified"`
|
||||
}
|
||||
if err := get("/user/emails", &emails); err != nil {
|
||||
return Claims{}, err
|
||||
}
|
||||
cl := Claims{Subject: strconv.FormatInt(user.ID, 10), Name: user.Name}
|
||||
if cl.Name == "" {
|
||||
cl.Name = user.Login
|
||||
}
|
||||
for _, e := range emails {
|
||||
if e.Primary {
|
||||
cl.Email, cl.EmailVerified = e.Email, e.Verified
|
||||
}
|
||||
}
|
||||
return cl, nil
|
||||
}
|
||||
+16
-1
@@ -16,8 +16,10 @@ import (
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
// Provider is one organization's IdP registration.
|
||||
// Provider is an IdP registration, of an organization or the platform.
|
||||
type Provider struct {
|
||||
// Kind is empty for OpenID Connect or GitHub.
|
||||
Kind string
|
||||
Issuer string
|
||||
ClientID string
|
||||
ClientSecret string
|
||||
@@ -91,6 +93,9 @@ func (c *Client) ValidateIssuer(ctx context.Context, issuer string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// CheckURL checks that a server URL may be used, without contacting it.
|
||||
func (c *Client) CheckURL(issuer string) error { return c.checkIssuer(issuer) }
|
||||
|
||||
func (c *Client) checkIssuer(issuer string) error {
|
||||
u, err := url.Parse(issuer)
|
||||
if err != nil || u.Host == "" || u.RawQuery != "" || u.Fragment != "" || u.User != nil {
|
||||
@@ -144,6 +149,9 @@ func (c *Client) AuthURL(ctx context.Context, p Provider, f Flow) (string, error
|
||||
if err := c.checkIssuer(p.Issuer); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if p.Kind == GitHub {
|
||||
return c.githubOAuth(p).AuthCodeURL(f.State, oauth2.S256ChallengeOption(f.Verifier)), nil
|
||||
}
|
||||
op, err := c.discover(ctx, p.Issuer)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -157,6 +165,13 @@ func (c *Client) AuthURL(ctx context.Context, p Provider, f Flow) (string, error
|
||||
// Exchange redeems the authorization code and verifies the ID token's
|
||||
// signature, issuer, audience, expiry and nonce.
|
||||
func (c *Client) Exchange(ctx context.Context, p Provider, f Flow, code string) (Claims, error) {
|
||||
if p.Kind == GitHub {
|
||||
tok, err := c.githubOAuth(p).Exchange(oidc.ClientContext(ctx, c.http), code, oauth2.VerifierOption(f.Verifier))
|
||||
if err != nil {
|
||||
return Claims{}, fmt.Errorf("sso: code exchange: %w", err)
|
||||
}
|
||||
return c.githubClaims(ctx, p, tok)
|
||||
}
|
||||
op, err := c.discover(ctx, p.Issuer)
|
||||
if err != nil {
|
||||
return Claims{}, err
|
||||
|
||||
Reference in new issue
Block a user