Files
upstream-ory-kratos/selfservice/strategy/oidc/provider_gitlab.go
upstream-mirror c60cc7ca75
Some checks failed
CI / Run tests and lints (push) Failing after 29s
CI / Build CLI docs (push) Has been skipped
CI / Run Playwright end-to-end tests (mysql) (push) Failing after 3s
CI / Run Playwright end-to-end tests (postgres) (push) Failing after 2s
CI / Run Playwright end-to-end tests (sqlite) (push) Failing after 3s
CodeQL / Analyze (go) (push) Failing after 4s
CodeQL / Analyze (javascript) (push) Failing after 3s
Docker Image Scanners / scanners (push) Failing after 4s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Run end-to-end tests (postgres) (push) Failing after 19s
CI / Run end-to-end tests (sqlite) (push) Failing after 2s
CI / Run Playwright end-to-end tests (cockroach) (push) Failing after 2s
CI / Draft newsletter (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
snapshot of https://github.com/ory/kratos.git (master, 2026-09-15; blobs > 131072B omitted)
2026-09-15 21:19:12 +05:00

118 lines
2.8 KiB
Go

// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package oidc
import (
"cmp"
"context"
"encoding/json"
"net/url"
"path"
"github.com/hashicorp/go-retryablehttp"
"github.com/ory/x/httpx"
"github.com/pkg/errors"
"golang.org/x/oauth2"
"github.com/ory/herodot"
)
const (
defaultEndpoint = "https://gitlab.com"
)
var _ OAuth2Provider = (*ProviderGitLab)(nil)
type ProviderGitLab struct {
*ProviderGenericOIDC
}
func NewProviderGitLab(
config *Configuration,
reg Dependencies,
) Provider {
return &ProviderGitLab{
ProviderGenericOIDC: &ProviderGenericOIDC{
config: config,
reg: reg,
},
}
}
func (g *ProviderGitLab) oauth2(ctx context.Context) (*oauth2.Config, error) {
endpoint, err := g.endpoint()
if err != nil {
return nil, errors.WithStack(herodot.ErrInternalServerError().WithWrap(err).WithReasonf("%s", err))
}
authURL := *endpoint
tokenURL := *endpoint
authURL.Path = path.Join(authURL.Path, "/oauth/authorize")
tokenURL.Path = path.Join(tokenURL.Path, "/oauth/token")
return &oauth2.Config{
ClientID: g.config.ClientID,
ClientSecret: g.config.ClientSecret,
Endpoint: oauth2.Endpoint{
AuthURL: authURL.String(),
TokenURL: tokenURL.String(),
},
Scopes: g.config.Scope,
RedirectURL: g.config.Redir(g.reg.Config().OIDCRedirectURIBase(ctx)),
}, nil
}
func (g *ProviderGitLab) OAuth2(ctx context.Context) (*oauth2.Config, error) {
return g.oauth2(ctx)
}
func (g *ProviderGitLab) Claims(ctx context.Context, exchange *oauth2.Token, query url.Values) (*Claims, error) {
o, err := g.OAuth2(ctx)
if err != nil {
return nil, err
}
u, err := g.endpoint()
if err != nil {
return nil, errors.WithStack(herodot.ErrInternalServerError().WithWrap(err).WithReasonf("%s", err))
}
u.Path = path.Join(u.Path, "/oauth/userinfo")
ctx, client := httpx.SetOAuth2(ctx, g.reg.HTTPClient(ctx), o, exchange)
req, err := retryablehttp.NewRequestWithContext(ctx, "GET", u.String(), nil)
if err != nil {
return nil, errors.WithStack(herodot.ErrInternalServerError().WithWrap(err).WithReasonf("%s", err))
}
req.Header.Set("Accept", "application/json")
resp, err := client.Do(req)
if err != nil {
return nil, errors.WithStack(herodot.ErrUpstreamError().WithWrap(err).WithReasonf("%s", err))
}
defer func() { _ = resp.Body.Close() }()
if err := logUpstreamError(g.reg.Logger(), resp); err != nil {
return nil, err
}
var claims Claims
if err := json.NewDecoder(resp.Body).Decode(&claims); err != nil {
return nil, errors.WithStack(herodot.ErrUpstreamError().WithWrap(err).WithReasonf("%s", err))
}
claims.Issuer = cmp.Or(claims.Issuer, g.config.IssuerURL)
return &claims, nil
}
func (g *ProviderGitLab) endpoint() (*url.URL, error) {
e := defaultEndpoint
if len(g.config.IssuerURL) > 0 {
e = g.config.IssuerURL
}
return url.Parse(e)
}