2023-07-10 13:27:00 +00:00
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2023-10-17 15:19:51 +00:00
|
|
|
"github.com/zitadel/oidc/v3/pkg/client"
|
|
|
|
"github.com/zitadel/oidc/v3/pkg/client/rp"
|
|
|
|
"github.com/zitadel/oidc/v3/pkg/client/rs"
|
|
|
|
"github.com/zitadel/oidc/v3/pkg/oidc"
|
2023-07-10 13:27:00 +00:00
|
|
|
|
|
|
|
http_util "github.com/zitadel/zitadel/internal/api/http"
|
|
|
|
oidc_internal "github.com/zitadel/zitadel/internal/api/oidc"
|
|
|
|
"github.com/zitadel/zitadel/pkg/grpc/app"
|
|
|
|
"github.com/zitadel/zitadel/pkg/grpc/management"
|
|
|
|
)
|
|
|
|
|
2023-07-19 11:17:39 +00:00
|
|
|
func (s *Tester) CreateOIDCNativeClient(ctx context.Context, redirectURI, logoutRedirectURI, projectID string) (*management.AddOIDCAppResponse, error) {
|
2023-07-10 13:27:00 +00:00
|
|
|
return s.Client.Mgmt.AddOIDCApp(ctx, &management.AddOIDCAppRequest{
|
2023-07-14 11:16:16 +00:00
|
|
|
ProjectId: projectID,
|
2023-07-10 13:27:00 +00:00
|
|
|
Name: fmt.Sprintf("app-%d", time.Now().UnixNano()),
|
|
|
|
RedirectUris: []string{redirectURI},
|
|
|
|
ResponseTypes: []app.OIDCResponseType{app.OIDCResponseType_OIDC_RESPONSE_TYPE_CODE},
|
|
|
|
GrantTypes: []app.OIDCGrantType{app.OIDCGrantType_OIDC_GRANT_TYPE_AUTHORIZATION_CODE, app.OIDCGrantType_OIDC_GRANT_TYPE_REFRESH_TOKEN},
|
|
|
|
AppType: app.OIDCAppType_OIDC_APP_TYPE_NATIVE,
|
|
|
|
AuthMethodType: app.OIDCAuthMethodType_OIDC_AUTH_METHOD_TYPE_NONE,
|
2023-07-19 11:17:39 +00:00
|
|
|
PostLogoutRedirectUris: []string{logoutRedirectURI},
|
2023-07-10 13:27:00 +00:00
|
|
|
Version: app.OIDCVersion_OIDC_VERSION_1_0,
|
|
|
|
DevMode: false,
|
|
|
|
AccessTokenType: app.OIDCTokenType_OIDC_TOKEN_TYPE_JWT,
|
|
|
|
AccessTokenRoleAssertion: false,
|
|
|
|
IdTokenRoleAssertion: false,
|
|
|
|
IdTokenUserinfoAssertion: false,
|
|
|
|
ClockSkew: nil,
|
|
|
|
AdditionalOrigins: nil,
|
|
|
|
SkipNativeAppSuccessPage: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Tester) CreateOIDCImplicitFlowClient(ctx context.Context, redirectURI string) (*management.AddOIDCAppResponse, error) {
|
|
|
|
project, err := s.Client.Mgmt.AddProject(ctx, &management.AddProjectRequest{
|
|
|
|
Name: fmt.Sprintf("project-%d", time.Now().UnixNano()),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return s.Client.Mgmt.AddOIDCApp(ctx, &management.AddOIDCAppRequest{
|
|
|
|
ProjectId: project.GetId(),
|
|
|
|
Name: fmt.Sprintf("app-%d", time.Now().UnixNano()),
|
|
|
|
RedirectUris: []string{redirectURI},
|
|
|
|
ResponseTypes: []app.OIDCResponseType{app.OIDCResponseType_OIDC_RESPONSE_TYPE_ID_TOKEN_TOKEN},
|
|
|
|
GrantTypes: []app.OIDCGrantType{app.OIDCGrantType_OIDC_GRANT_TYPE_IMPLICIT},
|
|
|
|
AppType: app.OIDCAppType_OIDC_APP_TYPE_USER_AGENT,
|
|
|
|
AuthMethodType: app.OIDCAuthMethodType_OIDC_AUTH_METHOD_TYPE_NONE,
|
|
|
|
PostLogoutRedirectUris: nil,
|
|
|
|
Version: app.OIDCVersion_OIDC_VERSION_1_0,
|
|
|
|
DevMode: true,
|
|
|
|
AccessTokenType: app.OIDCTokenType_OIDC_TOKEN_TYPE_JWT,
|
|
|
|
AccessTokenRoleAssertion: false,
|
|
|
|
IdTokenRoleAssertion: false,
|
|
|
|
IdTokenUserinfoAssertion: false,
|
|
|
|
ClockSkew: nil,
|
|
|
|
AdditionalOrigins: nil,
|
|
|
|
SkipNativeAppSuccessPage: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-14 11:16:16 +00:00
|
|
|
func (s *Tester) CreateProject(ctx context.Context) (*management.AddProjectResponse, error) {
|
|
|
|
return s.Client.Mgmt.AddProject(ctx, &management.AddProjectRequest{
|
|
|
|
Name: fmt.Sprintf("project-%d", time.Now().UnixNano()),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Tester) CreateAPIClient(ctx context.Context, projectID string) (*management.AddAPIAppResponse, error) {
|
|
|
|
return s.Client.Mgmt.AddAPIApp(ctx, &management.AddAPIAppRequest{
|
|
|
|
ProjectId: projectID,
|
|
|
|
Name: fmt.Sprintf("api-%d", time.Now().UnixNano()),
|
|
|
|
AuthMethodType: app.APIAuthMethodType_API_AUTH_METHOD_TYPE_PRIVATE_KEY_JWT,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-10-17 15:19:51 +00:00
|
|
|
func (s *Tester) CreateOIDCAuthRequest(ctx context.Context, clientID, loginClient, redirectURI string, scope ...string) (authRequestID string, err error) {
|
|
|
|
provider, err := s.CreateRelyingParty(ctx, clientID, redirectURI, scope...)
|
2023-07-10 13:27:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
codeVerifier := "codeVerifier"
|
|
|
|
codeChallenge := oidc.NewSHACodeChallenge(codeVerifier)
|
|
|
|
authURL := rp.AuthURL("state", provider, rp.WithCodeChallenge(codeChallenge))
|
|
|
|
|
2023-09-29 09:26:14 +00:00
|
|
|
req, err := GetRequest(authURL, map[string]string{oidc_internal.LoginClientHeader: loginClient})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
loc, err := CheckRedirect(req)
|
2023-07-10 13:27:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
prefixWithHost := provider.Issuer() + s.Config.OIDC.DefaultLoginURLV2
|
|
|
|
if !strings.HasPrefix(loc.String(), prefixWithHost) {
|
|
|
|
return "", fmt.Errorf("login location has not prefix %s, but is %s", prefixWithHost, loc.String())
|
|
|
|
}
|
|
|
|
return strings.TrimPrefix(loc.String(), prefixWithHost), nil
|
|
|
|
}
|
|
|
|
|
2023-10-17 15:19:51 +00:00
|
|
|
func (s *Tester) CreateOIDCAuthRequestImplicit(ctx context.Context, clientID, loginClient, redirectURI string, scope ...string) (authRequestID string, err error) {
|
|
|
|
provider, err := s.CreateRelyingParty(ctx, clientID, redirectURI, scope...)
|
2023-07-10 13:27:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
authURL := rp.AuthURL("state", provider)
|
|
|
|
|
|
|
|
// implicit is not natively supported so let's just overwrite the response type
|
|
|
|
parsed, _ := url.Parse(authURL)
|
|
|
|
queries := parsed.Query()
|
|
|
|
queries.Set("response_type", string(oidc.ResponseTypeIDToken))
|
|
|
|
parsed.RawQuery = queries.Encode()
|
|
|
|
authURL = parsed.String()
|
|
|
|
|
2023-09-29 09:26:14 +00:00
|
|
|
req, err := GetRequest(authURL, map[string]string{oidc_internal.LoginClientHeader: loginClient})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
loc, err := CheckRedirect(req)
|
2023-07-10 13:27:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
prefixWithHost := provider.Issuer() + s.Config.OIDC.DefaultLoginURLV2
|
|
|
|
if !strings.HasPrefix(loc.String(), prefixWithHost) {
|
|
|
|
return "", fmt.Errorf("login location has not prefix %s, but is %s", prefixWithHost, loc.String())
|
|
|
|
}
|
|
|
|
return strings.TrimPrefix(loc.String(), prefixWithHost), nil
|
|
|
|
}
|
|
|
|
|
2023-07-14 11:16:16 +00:00
|
|
|
func (s *Tester) OIDCIssuer() string {
|
|
|
|
return http_util.BuildHTTP(s.Config.ExternalDomain, s.Config.Port, s.Config.ExternalSecure)
|
|
|
|
}
|
|
|
|
|
2023-10-17 15:19:51 +00:00
|
|
|
func (s *Tester) CreateRelyingParty(ctx context.Context, clientID, redirectURI string, scope ...string) (rp.RelyingParty, error) {
|
2023-07-10 13:27:00 +00:00
|
|
|
if len(scope) == 0 {
|
|
|
|
scope = []string{oidc.ScopeOpenID}
|
|
|
|
}
|
2023-07-19 11:17:39 +00:00
|
|
|
loginClient := &http.Client{Transport: &loginRoundTripper{http.DefaultTransport}}
|
2023-10-17 15:19:51 +00:00
|
|
|
return rp.NewRelyingPartyOIDC(ctx, s.OIDCIssuer(), clientID, "", redirectURI, scope, rp.WithHTTPClient(loginClient))
|
2023-07-19 11:17:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type loginRoundTripper struct {
|
|
|
|
http.RoundTripper
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *loginRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
|
|
req.Header.Set(oidc_internal.LoginClientHeader, LoginUser)
|
|
|
|
return c.RoundTripper.RoundTrip(req)
|
2023-07-14 11:16:16 +00:00
|
|
|
}
|
|
|
|
|
2023-10-17 15:19:51 +00:00
|
|
|
func (s *Tester) CreateResourceServer(ctx context.Context, keyFileData []byte) (rs.ResourceServer, error) {
|
2023-07-14 11:16:16 +00:00
|
|
|
keyFile, err := client.ConfigFromKeyFileData(keyFileData)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-10-17 15:19:51 +00:00
|
|
|
return rs.NewResourceServerJWTProfile(ctx, s.OIDCIssuer(), keyFile.ClientID, keyFile.KeyID, []byte(keyFile.Key))
|
2023-07-10 13:27:00 +00:00
|
|
|
}
|
|
|
|
|
2023-09-29 09:26:14 +00:00
|
|
|
func GetRequest(url string, headers map[string]string) (*http.Request, error) {
|
2023-07-10 13:27:00 +00:00
|
|
|
req, err := http.NewRequest(http.MethodGet, url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for key, value := range headers {
|
|
|
|
req.Header.Set(key, value)
|
|
|
|
}
|
2023-09-29 09:26:14 +00:00
|
|
|
return req, nil
|
|
|
|
}
|
2023-07-10 13:27:00 +00:00
|
|
|
|
2023-09-29 09:26:14 +00:00
|
|
|
func CheckRedirect(req *http.Request) (*url.URL, error) {
|
2023-07-10 13:27:00 +00:00
|
|
|
client := &http.Client{
|
|
|
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
|
|
|
return http.ErrUseLastResponse
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return resp.Location()
|
|
|
|
}
|