Platform admins register identity providers for the whole installation: any OpenID Connect provider, checked by discovery, and GitHub or GitHub Enterprise through OAuth, which reads the primary email GitHub verified. Sign-in pages offer them; the sign-up page offers those open to sign-up, also where email sign-up is closed for lack of a mail server. Accounts are reached only through identities stored by subject. A new identity signs up only with an email the provider confirmed, within the per-address limit and sign-up mode, and never takes over an existing account, including through an alias of its address: its owner links the provider in the security settings. The account's TOTP still applies after the provider. Unlinking refuses to remove the last way to sign in, and removing a provider refuses while accounts depend on it. The test browser takes the CSRF token from pages, as the console does. Closes #114 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
94 lines
2.5 KiB
Go
94 lines
2.5 KiB
Go
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
|
|
}
|