mirror of
https://github.com/hanzoai/ca.git
synced 2026-11-03 18:08:27 +00:00
1352 lines
40 KiB
Go
1352 lines
40 KiB
Go
package auth_test
|
|
|
|
import (
|
|
"context"
|
|
"crypto/ecdsa"
|
|
"crypto/elliptic"
|
|
"crypto/rand"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/cockroachdb/errors"
|
|
"github.com/effective-security/porto/gserver"
|
|
"github.com/effective-security/porto/gserver/roles"
|
|
"github.com/effective-security/porto/pkg/retriable"
|
|
"github.com/effective-security/porto/xhttp/header"
|
|
"github.com/effective-security/porto/xhttp/identity"
|
|
"github.com/effective-security/servefiles"
|
|
"github.com/effective-security/trustyca/api/pb"
|
|
"github.com/effective-security/trustyca/api/pb/proxypb"
|
|
"github.com/effective-security/trustyca/internal/config"
|
|
"github.com/effective-security/trustyca/internal/db"
|
|
"github.com/effective-security/trustyca/server/appcontainer"
|
|
"github.com/effective-security/trustyca/server/service/auth"
|
|
"github.com/effective-security/trustyca/tests/testutils"
|
|
"github.com/effective-security/x/urlutil"
|
|
"github.com/effective-security/xpki/certutil"
|
|
"github.com/effective-security/xpki/dataprotection"
|
|
"github.com/effective-security/xpki/jwt"
|
|
"github.com/effective-security/xpki/jwt/dpop"
|
|
"github.com/effective-security/xpki/jwt/oauth2client"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"google.golang.org/protobuf/types/known/emptypb"
|
|
)
|
|
|
|
var (
|
|
// testServer runs the one auth service of the package. Tests that
|
|
// change its configuration - BaseURL and the providers registered with
|
|
// SetAuthProvider - must not call t.Parallel: they run in the
|
|
// sequential phase, before the parallel tests that read it resume.
|
|
testServer gserver.GServer
|
|
dpopSigner dpop.Signer
|
|
authClient pb.AuthServer
|
|
httpAddr string
|
|
// testDb and testDp are the DB and data protection of the test
|
|
// server, for fixtures and to read the events it records
|
|
testDb db.OrgsDb
|
|
testDp dataprotection.Provider
|
|
// testCfg is the configuration of the service. A test that changes it
|
|
// must not call t.Parallel, as for BaseURL.
|
|
testCfg *config.Configuration
|
|
// testCookies are the cookies of the wfe of the dev configuration,
|
|
// which the test server sets
|
|
testCookies roles.CookiesConfig
|
|
)
|
|
|
|
var jsonContentHeaders = map[string]string{
|
|
header.Accept: header.ApplicationJSON,
|
|
header.ContentType: header.ApplicationJSON,
|
|
}
|
|
|
|
var textContentHeaders = map[string]string{
|
|
header.Accept: header.TextPlain,
|
|
header.ContentType: header.ApplicationJSON,
|
|
}
|
|
|
|
// serviceFactories provides map of gserver.ServiceFactory
|
|
var serviceFactories = map[string]gserver.ServiceFactory{
|
|
auth.ServiceName: auth.Factory,
|
|
}
|
|
|
|
func TestMain(m *testing.M) {
|
|
var err error
|
|
|
|
httpAddr = testutils.CreateURL("http", "")
|
|
|
|
closer := appcontainer.NewCloser(4)
|
|
f := appcontainer.NewContainerFactory(closer).
|
|
WithConfigurationProvider(func() (*config.Configuration, error) {
|
|
return testutils.LoadConfig("UNIT_TEST")
|
|
})
|
|
container, err := f.CreateContainerWithDependencies()
|
|
if err != nil {
|
|
panic(errors.WithStack(err))
|
|
}
|
|
err = container.Invoke(func(orgsDb db.OrgsDb, dp dataprotection.Provider, svcCfg *config.Configuration) {
|
|
testDb = orgsDb
|
|
testDp = dp
|
|
testCfg = svcCfg
|
|
})
|
|
if err != nil {
|
|
panic(errors.WithStack(err))
|
|
}
|
|
|
|
testCookies = testCfg.HTTPServers[config.WFEServerName].IdentityMap.GetCookiesConfig()
|
|
cfg := &gserver.Config{
|
|
ListenURLs: []string{httpAddr},
|
|
Services: []string{auth.ServiceName},
|
|
IdentityMap: &roles.IdentityMap{
|
|
Cookies: testCookies,
|
|
},
|
|
}
|
|
|
|
testServer, err = gserver.Start("AuthTest", cfg, container, serviceFactories)
|
|
if err != nil || testServer == nil {
|
|
panic(errors.WithStack(err))
|
|
}
|
|
|
|
serviceServer := testServer.Service(auth.ServiceName).(pb.AuthServer)
|
|
authClient = proxypb.NewAuthClientFromProxy(proxypb.AuthServerToClient(serviceServer))
|
|
|
|
ecKey, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
dpopSigner, err = dpop.NewSigner(ecKey)
|
|
if err != nil || dpopSigner == nil {
|
|
panic(errors.WithStack(err))
|
|
}
|
|
|
|
os.Setenv("TRUSTYCA_GITHUB_CLIENT_ID", "testclientid")
|
|
os.Setenv("TRUSTYCA_GITHUB_CLIENT_SECRET", "testclientsecret")
|
|
os.Setenv("TRUSTYCA_GOOGLE_CLIENT_ID", "testclientid")
|
|
os.Setenv("TRUSTYCA_GOOGLE_CLIENT_SECRET", "testclientsecret")
|
|
|
|
// Run the tests
|
|
rc := m.Run()
|
|
|
|
// cleanup
|
|
testServer.Close()
|
|
closer.Close()
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
os.Exit(rc)
|
|
}
|
|
|
|
func TestIsReady(t *testing.T) {
|
|
t.Parallel()
|
|
service := testServer.Service(auth.ServiceName).(*auth.Service)
|
|
require.NotNil(t, service)
|
|
assert.True(t, service.IsReady())
|
|
|
|
ctx := context.Background()
|
|
_, err := service.AuthProvider(ctx, pb.IDP_Github)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
func TestClose(t *testing.T) {
|
|
t.Parallel()
|
|
service := new(auth.Service)
|
|
assert.NotPanics(t, service.Close)
|
|
assert.NotPanics(t, service.Close)
|
|
}
|
|
|
|
func TestFactory(t *testing.T) {
|
|
t.Parallel()
|
|
assert.Panics(t, func() {
|
|
auth.Factory(nil)
|
|
})
|
|
}
|
|
|
|
func Test_AuthProvidersHandler(t *testing.T) {
|
|
t.Parallel()
|
|
service := testServer.Service(auth.ServiceName).(*auth.Service)
|
|
require.NotNil(t, service)
|
|
|
|
h := service.AuthProvidersHandler()
|
|
w := httptest.NewRecorder()
|
|
r, err := http.NewRequest(http.MethodGet, pb.PathForAuthProviders, nil)
|
|
require.NoError(t, err)
|
|
|
|
h(w, r, nil)
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
|
|
|
var res pb.AuthProvidersResponse
|
|
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &res))
|
|
assert.NotEmpty(t, res.AuthURL)
|
|
assert.NotEmpty(t, res.Providers)
|
|
}
|
|
|
|
func Test_AuthProvidersHandlerPost(t *testing.T) {
|
|
t.Parallel()
|
|
service := testServer.Service(auth.ServiceName).(*auth.Service)
|
|
require.NotNil(t, service)
|
|
|
|
h := service.AuthProvidersHandler()
|
|
req := &pb.AuthProvidersRequest{
|
|
Email: "",
|
|
}
|
|
|
|
w, r, err := testutils.CreateRequest(http.MethodPost, pb.PathForAuthProviders, req, nil)
|
|
require.NoError(t, err)
|
|
|
|
h(w, r, nil)
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
|
|
|
var res pb.AuthProvidersResponse
|
|
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &res))
|
|
assert.NotEmpty(t, res.AuthURL)
|
|
assert.NotEmpty(t, res.Providers)
|
|
}
|
|
|
|
func Test_CallbackHandler(t *testing.T) {
|
|
// not parallel: it changes BaseURL and the Github provider of testServer
|
|
service := testServer.Service(auth.ServiceName).(*auth.Service)
|
|
require.NotNil(t, service)
|
|
|
|
h := service.CallbackHandler()
|
|
|
|
server := servefiles.New(t)
|
|
server.SetBaseDirs("testdata")
|
|
ctx := context.Background()
|
|
|
|
baseURL := server.URL()
|
|
o := &oauth2client.ClientConfig{
|
|
ProviderID: strings.ToLower(pb.IDP_Github.String()),
|
|
ClientID: "1234",
|
|
ClientSecret: "1234",
|
|
Scopes: []string{"email"},
|
|
RedirectURL: "http://localhost:38989",
|
|
ResponseType: pb.OAuthResponseTypeCode,
|
|
AuthURL: baseURL + "/login/oauth/authorize",
|
|
TokenURL: baseURL + "/login/oauth/access_token",
|
|
UserinfoURL: baseURL + "/user",
|
|
}
|
|
|
|
err := service.SetAuthProvider(ctx, o)
|
|
require.NoError(t, err)
|
|
|
|
u, err := url.Parse(baseURL + "/")
|
|
require.NoError(t, err)
|
|
|
|
service.BaseURL = u
|
|
|
|
t.Run("no_code", func(t *testing.T) {
|
|
t.Parallel()
|
|
w := httptest.NewRecorder()
|
|
r, err := http.NewRequest(http.MethodGet, pb.PathForAuthCallback, nil)
|
|
require.NoError(t, err)
|
|
|
|
h(w, r, nil)
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
assert.Equal(t, `{"code":"invalid_request","message":"missing state parameter"}`, w.Body.String())
|
|
})
|
|
|
|
t.Run("no_state", func(t *testing.T) {
|
|
t.Parallel()
|
|
w := httptest.NewRecorder()
|
|
r, err := http.NewRequest(http.MethodGet, pb.PathForAuthCallback+"?code=abc", nil)
|
|
require.NoError(t, err)
|
|
|
|
h(w, r, nil)
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
assert.Equal(t, "{\"code\":\"invalid_request\",\"message\":\"missing state parameter\"}", w.Body.String())
|
|
})
|
|
|
|
t.Run("bad_state_decode", func(t *testing.T) {
|
|
w := httptest.NewRecorder()
|
|
r, err := http.NewRequest(http.MethodGet, pb.PathForAuthCallback+"?code=abc&state=lll", nil)
|
|
require.NoError(t, err)
|
|
|
|
h(w, r, nil)
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
assert.Equal(t, `{"code":"invalid_request","message":"failed to decrypt state"}`, w.Body.String())
|
|
})
|
|
|
|
t.Run("bad_state_base64", func(t *testing.T) {
|
|
w := httptest.NewRecorder()
|
|
r, err := http.NewRequest(http.MethodGet, pb.PathForAuthCallback+"?code=abc&state=_", nil)
|
|
require.NoError(t, err)
|
|
|
|
h(w, r, nil)
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
assert.Equal(t, `{"code":"invalid_request","message":"failed to decrypt state"}`, w.Body.String())
|
|
})
|
|
|
|
t.Run("token", func(t *testing.T) {
|
|
state, err := service.Protect(context.Background(), &auth.State{
|
|
ResponseType: "token",
|
|
RedirectURL: "https://localhost:8880/v1/auth",
|
|
Provider: pb.IDP_Github,
|
|
IssuedAt: time.Now().Unix(),
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
w := httptest.NewRecorder()
|
|
// Value of code is not magic. Mock configured in requests.json will ignore code and give back a token.
|
|
r, err := http.NewRequest(http.MethodGet, pb.PathForAuthCallback+"?code=9298935ecf8777061ff2&state="+state, nil)
|
|
require.NoError(t, err)
|
|
|
|
h(w, r, nil)
|
|
require.Equal(t, http.StatusSeeOther, w.Code)
|
|
loc := w.Header().Get("Location")
|
|
assert.NotEmpty(t, loc)
|
|
})
|
|
}
|
|
|
|
func Test_DPoPAuthorizeHandler(t *testing.T) {
|
|
service := testServer.Service(auth.ServiceName).(*auth.Service)
|
|
require.NotNil(t, service)
|
|
|
|
//ctx := context.Background()
|
|
h := service.AuthorizeHandler()
|
|
|
|
t.Run("no_idp", func(t *testing.T) {
|
|
t.Parallel()
|
|
w := httptest.NewRecorder()
|
|
|
|
params := url.Values{
|
|
"client_id": {"trustyca"},
|
|
}
|
|
r, err := http.NewRequest(http.MethodGet, pb.PathForAuthorize+"?"+params.Encode(), nil)
|
|
require.NoError(t, err)
|
|
|
|
h(w, r, nil)
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
assert.Equal(t, `{"code":"invalid_request","message":"unsupported IDP"}`, w.Body.String())
|
|
})
|
|
|
|
t.Run("no_redirect_uri_parameter", func(t *testing.T) {
|
|
t.Parallel()
|
|
w := httptest.NewRecorder()
|
|
|
|
params := url.Values{
|
|
"client_id": {"trustyca"},
|
|
"provider": {pb.IDP_Github.DisplayName()},
|
|
}
|
|
r, err := http.NewRequest(http.MethodGet, pb.PathForAuthorize+"?"+params.Encode(), nil)
|
|
require.NoError(t, err)
|
|
|
|
h(w, r, nil)
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
assert.Equal(t, `{"code":"invalid_request","message":"missing parameter: redirect_uri"}`, w.Body.String())
|
|
})
|
|
|
|
t.Run("invalid_client", func(t *testing.T) {
|
|
t.Parallel()
|
|
w := httptest.NewRecorder()
|
|
|
|
params := url.Values{
|
|
"client_id": {"123"},
|
|
"response_mode": {"query"},
|
|
"response_type": {"code"},
|
|
"scope": {"email"},
|
|
"redirect_uri": {"http://localhost:38989"},
|
|
"provider": {pb.IDP_Github.DisplayName()},
|
|
"nonce": {"123"},
|
|
}
|
|
|
|
r, err := http.NewRequest(http.MethodGet, pb.PathForAuthorize+"?"+params.Encode(), nil)
|
|
require.NoError(t, err)
|
|
|
|
h(w, r, nil)
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
assert.Equal(t, `{"code":"invalid_request","message":"unsupported client ID: 123"}`, w.Body.String())
|
|
})
|
|
|
|
t.Run("invalid_prov", func(t *testing.T) {
|
|
t.Parallel()
|
|
w := httptest.NewRecorder()
|
|
|
|
params := url.Values{
|
|
"client_id": {"trustyca"},
|
|
"response_mode": {"query"},
|
|
"response_type": {"token"},
|
|
"scope": {"email"},
|
|
"redirect_uri": {"http://localhost:38989"},
|
|
"provider": {"SAML123"},
|
|
"nonce": {"123"},
|
|
}
|
|
|
|
r, err := http.NewRequest(http.MethodGet, pb.PathForAuthorize+"?"+params.Encode(), nil)
|
|
require.NoError(t, err)
|
|
|
|
h(w, r, nil)
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
assert.Equal(t, `{"code":"invalid_request","message":"unsupported IDP"}`, w.Body.String())
|
|
})
|
|
|
|
t.Run("invalid_response_type", func(t *testing.T) {
|
|
t.Parallel()
|
|
w := httptest.NewRecorder()
|
|
|
|
params := url.Values{
|
|
"client_id": {"trustyca"},
|
|
"response_mode": {"query"},
|
|
"response_type": {"code"},
|
|
"scope": {"email"},
|
|
"redirect_uri": {"http://localhost:38989"},
|
|
"provider": {"SAML123"},
|
|
"nonce": {"123"},
|
|
}
|
|
|
|
r, err := http.NewRequest(http.MethodGet, pb.PathForAuthorize+"?"+params.Encode(), nil)
|
|
require.NoError(t, err)
|
|
|
|
h(w, r, nil)
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
assert.Equal(t, `{"code":"invalid_request","message":"unsupported IDP"}`, w.Body.String())
|
|
})
|
|
|
|
t.Run("url", func(t *testing.T) {
|
|
t.Parallel()
|
|
w := httptest.NewRecorder()
|
|
|
|
params := url.Values{
|
|
"client_id": {"trustyca"},
|
|
"response_mode": {"query"},
|
|
"response_type": {"token"},
|
|
"scope": {"email"},
|
|
"redirect_uri": {"http://localhost:38989"},
|
|
"provider": {pb.IDP_Github.DisplayName()},
|
|
"nonce": {"123"},
|
|
}
|
|
|
|
r, err := http.NewRequest(http.MethodGet, pb.PathForAuthorize+"?"+params.Encode(), nil)
|
|
require.NoError(t, err)
|
|
|
|
h(w, r, nil)
|
|
assert.Equal(t, http.StatusSeeOther, w.Code)
|
|
})
|
|
|
|
t.Run("code_challenge_method", func(t *testing.T) {
|
|
t.Parallel()
|
|
w := httptest.NewRecorder()
|
|
params := url.Values{
|
|
"client_id": {"trustyca"},
|
|
"response_type": {"code"},
|
|
"redirect_uri": {"http://localhost:38989"},
|
|
"provider": {pb.IDP_Github.DisplayName()},
|
|
"code_challenge": {"challenge"},
|
|
"code_challenge_method": {"plain"},
|
|
}
|
|
r, err := http.NewRequest(http.MethodGet, pb.PathForAuthorize+"?"+params.Encode(), nil)
|
|
require.NoError(t, err)
|
|
|
|
h(w, r, nil)
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
assert.Equal(t, `{"code":"invalid_request","message":"unsupported code_challenge_method: plain"}`, w.Body.String())
|
|
})
|
|
|
|
t.Run("redirect_not_allowed", func(t *testing.T) {
|
|
t.Parallel()
|
|
w := httptest.NewRecorder()
|
|
params := url.Values{
|
|
"client_id": {"trustyca"},
|
|
"response_type": {"code"},
|
|
"redirect_uri": {"https://attacker.example.com/callback"},
|
|
"provider": {pb.IDP_Github.DisplayName()},
|
|
}
|
|
r, err := http.NewRequest(http.MethodGet, pb.PathForAuthorize+"?"+params.Encode(), nil)
|
|
require.NoError(t, err)
|
|
|
|
h(w, r, nil)
|
|
assert.Equal(t, http.StatusForbidden, w.Code)
|
|
assert.Equal(t, `{"code":"forbidden","message":"redirect_url not_allowed"}`, w.Body.String())
|
|
})
|
|
|
|
dpopParams := url.Values{
|
|
"client_id": {"trustyca"},
|
|
"response_type": {"code"},
|
|
"scope": {"openid email dpop"},
|
|
"redirect_uri": {"http://localhost:38989"},
|
|
"provider": {pb.IDP_Github.DisplayName()},
|
|
"nonce": {"123"},
|
|
}
|
|
authorizeURL := "https://localhost" + pb.PathForAuthorize + "?" + dpopParams.Encode()
|
|
|
|
t.Run("dpop_without_proof", func(t *testing.T) {
|
|
t.Parallel()
|
|
w := httptest.NewRecorder()
|
|
r, err := http.NewRequest(http.MethodGet, authorizeURL, nil)
|
|
require.NoError(t, err)
|
|
|
|
h(w, r, nil)
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
assert.Equal(t, `{"code":"invalid_request","message":"invalid DPoP proof"}`, w.Body.String())
|
|
})
|
|
|
|
t.Run("dpop_proof_for_other_uri", func(t *testing.T) {
|
|
t.Parallel()
|
|
w := httptest.NewRecorder()
|
|
r, err := http.NewRequest(http.MethodGet, authorizeURL, nil)
|
|
require.NoError(t, err)
|
|
other, err := url.Parse("https://localhost/v1/other")
|
|
require.NoError(t, err)
|
|
proof, err := dpopSigner.Sign(t.Context(), http.MethodGet, other, nil)
|
|
require.NoError(t, err)
|
|
r.Header.Set(dpop.HTTPHeader, proof)
|
|
|
|
h(w, r, nil)
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
assert.Equal(t, `{"code":"invalid_request","message":"invalid DPoP proof"}`, w.Body.String())
|
|
})
|
|
|
|
t.Run("dpop_proof", func(t *testing.T) {
|
|
t.Parallel()
|
|
w := httptest.NewRecorder()
|
|
r, err := http.NewRequest(http.MethodGet, authorizeURL, nil)
|
|
require.NoError(t, err)
|
|
_, err = dpop.ForRequest(dpopSigner, r, nil)
|
|
require.NoError(t, err)
|
|
|
|
h(w, r, nil)
|
|
require.Equal(t, http.StatusSeeOther, w.Code)
|
|
loc, err := url.Parse(w.Header().Get("Location"))
|
|
require.NoError(t, err)
|
|
|
|
var st auth.State
|
|
require.NoError(t, service.Unprotect(t.Context(), loc.Query().Get("state"), &st))
|
|
assert.Equal(t, dpopSigner.JWKThumbprint(), st.DPoPJwk)
|
|
})
|
|
}
|
|
|
|
func Test_DPoPCallbackHandlerError(t *testing.T) {
|
|
t.Parallel()
|
|
service := testServer.Service(auth.ServiceName).(*auth.Service)
|
|
require.NotNil(t, service)
|
|
|
|
h := service.CallbackHandler()
|
|
|
|
t.Run("unauthorized", func(t *testing.T) {
|
|
t.Parallel()
|
|
w := httptest.NewRecorder()
|
|
r, err := http.NewRequest(http.MethodGet, pb.PathForAuthCallback+"?error=unauthorized_client&error_description=invalid%20grant", nil)
|
|
require.NoError(t, err)
|
|
|
|
h(w, r, nil)
|
|
assert.Equal(t, http.StatusUnauthorized, w.Code)
|
|
assert.Equal(t, `{"code":"unauthorized_client","message":"OAuth provider error: invalid grant"}`, w.Body.String())
|
|
})
|
|
|
|
t.Run("temporarily_unavailable", func(t *testing.T) {
|
|
t.Parallel()
|
|
w := httptest.NewRecorder()
|
|
r, err := http.NewRequest(http.MethodGet, pb.PathForAuthCallback+"?error=temporarily_unavailable&error_description=try%20later", nil)
|
|
require.NoError(t, err)
|
|
|
|
h(w, r, nil)
|
|
assert.Equal(t, http.StatusServiceUnavailable, w.Code)
|
|
assert.Equal(t, `{"code":"temporarily_unavailable","message":"OAuth provider error: try later"}`, w.Body.String())
|
|
})
|
|
|
|
t.Run("server_error", func(t *testing.T) {
|
|
t.Parallel()
|
|
w := httptest.NewRecorder()
|
|
r, err := http.NewRequest(http.MethodGet, pb.PathForAuthCallback+"?error=server_error&error_description=try%20later", nil)
|
|
require.NoError(t, err)
|
|
|
|
h(w, r, nil)
|
|
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
|
assert.Equal(t, `{"code":"server_error","message":"OAuth provider error: try later"}`, w.Body.String())
|
|
})
|
|
}
|
|
|
|
func Test_DPoPCallbackHandler(t *testing.T) {
|
|
service := testServer.Service(auth.ServiceName).(*auth.Service)
|
|
require.NotNil(t, service)
|
|
|
|
ctx := context.Background()
|
|
h := service.CallbackHandler()
|
|
|
|
server := servefiles.New(t)
|
|
server.SetBaseDirs("testdata")
|
|
|
|
baseURL := server.URL()
|
|
o := &oauth2client.ClientConfig{
|
|
ProviderID: strings.ToLower(pb.IDP_Google.String()),
|
|
ClientID: "1234",
|
|
ClientSecret: "1234",
|
|
Scopes: []string{"email"},
|
|
RedirectURL: "http://localhost:38989",
|
|
ResponseType: pb.OAuthResponseTypeCode,
|
|
AuthURL: baseURL + "/o/oauth2/v2/auth",
|
|
TokenURL: baseURL + "/token",
|
|
UserinfoURL: baseURL + "/v1/userinfo",
|
|
}
|
|
|
|
// override the existing client
|
|
err := service.SetAuthProvider(ctx, o)
|
|
require.NoError(t, err)
|
|
|
|
u, err := url.Parse(baseURL + "/")
|
|
require.NoError(t, err)
|
|
service.BaseURL = u
|
|
|
|
t.Run("no_code_get", func(t *testing.T) {
|
|
t.Parallel()
|
|
w := httptest.NewRecorder()
|
|
r, err := http.NewRequest(http.MethodGet, pb.PathForAuthCallback+"?state=123", nil)
|
|
require.NoError(t, err)
|
|
|
|
h(w, r, nil)
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
assert.Equal(t, `{"code":"invalid_request","message":"failed to decrypt state"}`, w.Body.String())
|
|
})
|
|
|
|
t.Run("no_state_post", func(t *testing.T) {
|
|
t.Parallel()
|
|
params := url.Values{
|
|
"code": {"1123"},
|
|
}
|
|
|
|
w := httptest.NewRecorder()
|
|
r, err := http.NewRequest(http.MethodPost,
|
|
pb.PathForAuthCallback,
|
|
strings.NewReader(params.Encode()))
|
|
require.NoError(t, err)
|
|
r.Header.Add(header.ContentType, "application/x-www-form-urlencoded")
|
|
|
|
h(w, r, nil)
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
assert.Equal(t, `{"code":"invalid_request","message":"missing state parameter"}`, w.Body.String())
|
|
})
|
|
|
|
t.Run("invalid_state_post", func(t *testing.T) {
|
|
t.Parallel()
|
|
params := url.Values{
|
|
"code": {"1123"},
|
|
"state": {"1123"},
|
|
}
|
|
|
|
w := httptest.NewRecorder()
|
|
r, err := http.NewRequest(http.MethodPost,
|
|
pb.PathForAuthCallback,
|
|
strings.NewReader(params.Encode()))
|
|
require.NoError(t, err)
|
|
r.Header.Add(header.ContentType, "application/x-www-form-urlencoded")
|
|
|
|
h(w, r, nil)
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
assert.Equal(t, `{"code":"invalid_request","message":"failed to decrypt state"}`, w.Body.String())
|
|
})
|
|
|
|
idToken, err := service.JwtSigner.Sign(
|
|
ctx,
|
|
jwt.CreateClaims(
|
|
certutil.RandomString(8),
|
|
"[email protected]",
|
|
service.JwtSigner.Issuer(),
|
|
[]string{"http://localhost:38989"},
|
|
60*time.Minute,
|
|
jwt.MapClaims{
|
|
"sub": "[email protected]",
|
|
"picture": "https://bitly.io/2342134",
|
|
"email_verified": true,
|
|
},
|
|
))
|
|
require.NoError(t, err)
|
|
|
|
t.Run("valid_state_post_code_google", func(t *testing.T) {
|
|
t.Parallel()
|
|
st, err := service.Protect(ctx, &auth.State{
|
|
Provider: pb.IDP_Google,
|
|
ClientID: "1234",
|
|
Scope: "email",
|
|
Nonce: "8245027",
|
|
DPoPJwk: "1328450198456019",
|
|
Audience: []string{"http://localhost:38989"},
|
|
RedirectURL: "http://localhost:38989",
|
|
ResponseType: pb.OAuthResponseTypeCode,
|
|
IssuedAt: time.Now().Unix(),
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
params := url.Values{
|
|
"code": {"1123"},
|
|
"id_token": {idToken},
|
|
"state": {st},
|
|
}
|
|
|
|
w := httptest.NewRecorder()
|
|
r, err := http.NewRequest(http.MethodPost,
|
|
pb.PathForAuthCallback,
|
|
strings.NewReader(params.Encode()))
|
|
r.Header.Add(header.ContentType, "application/x-www-form-urlencoded")
|
|
require.NoError(t, err)
|
|
|
|
h(w, r, nil)
|
|
assert.Equal(t, http.StatusSeeOther, w.Code)
|
|
})
|
|
|
|
t.Run("valid_state_post_idtoken_google", func(t *testing.T) {
|
|
t.Parallel()
|
|
st, err := service.Protect(ctx, &auth.State{
|
|
Provider: pb.IDP_Google,
|
|
ClientID: "1234",
|
|
Scope: "email",
|
|
Nonce: "8245027",
|
|
Audience: []string{"http://localhost:38989"},
|
|
DPoPJwk: "82043985620196502917346501962350",
|
|
RedirectURL: "http://localhost:38989",
|
|
ResponseType: pb.OAuthResponseTypeIDToken,
|
|
IssuedAt: time.Now().Unix(),
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
params := url.Values{
|
|
"code": {"1123"},
|
|
"id_token": {idToken},
|
|
"state": {st},
|
|
}
|
|
|
|
w := httptest.NewRecorder()
|
|
r, err := http.NewRequest(http.MethodPost,
|
|
pb.PathForAuthCallback,
|
|
strings.NewReader(params.Encode()))
|
|
r.Header.Add(header.ContentType, "application/x-www-form-urlencoded")
|
|
require.NoError(t, err)
|
|
|
|
h(w, r, nil)
|
|
require.Equal(t, http.StatusSeeOther, w.Code)
|
|
loc, err := url.Parse(w.Header().Get(header.Location))
|
|
require.NoError(t, err)
|
|
assert.Equal(t, url.Values{
|
|
pb.OAuthParamError: {pb.OAuthErrorUnsupportedResponseType},
|
|
pb.OAuthParamErrorDescription: {"unsupported response_type: 'id_token'"},
|
|
}, loc.Query())
|
|
})
|
|
|
|
t.Run("valid_state_post_token_google", func(t *testing.T) {
|
|
t.Parallel()
|
|
st, err := service.Protect(ctx, &auth.State{
|
|
Provider: pb.IDP_Google,
|
|
ClientID: "1234",
|
|
Scope: "email",
|
|
Nonce: "8245027",
|
|
DPoPJwk: "1328450198456019",
|
|
Audience: []string{"http://localhost:38989"},
|
|
RedirectURL: "http://localhost:38989",
|
|
ResponseMode: "form_post",
|
|
ResponseType: "token",
|
|
IssuedAt: time.Now().Unix(),
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
params := url.Values{
|
|
"code": {"1123"},
|
|
"id_token": {idToken},
|
|
"state": {st},
|
|
}
|
|
|
|
w := httptest.NewRecorder()
|
|
r, err := http.NewRequest(http.MethodPost,
|
|
pb.PathForAuthCallback,
|
|
strings.NewReader(params.Encode()))
|
|
r.Header.Add(header.ContentType, "application/x-www-form-urlencoded")
|
|
require.NoError(t, err)
|
|
|
|
h(w, r, nil)
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
|
assert.Contains(t, w.Body.String(), "<title>Submit This Form</title>")
|
|
})
|
|
|
|
t.Run("valid_state_post_token_fagment", func(t *testing.T) {
|
|
t.Parallel()
|
|
st, err := service.Protect(ctx, &auth.State{
|
|
Provider: pb.IDP_Google,
|
|
ClientID: "1234",
|
|
Scope: "email",
|
|
Nonce: "8245027",
|
|
DPoPJwk: "1328450198456019",
|
|
Audience: []string{"http://localhost:38989"},
|
|
RedirectURL: "http://localhost:38989",
|
|
ResponseMode: "fragment",
|
|
ResponseType: "token",
|
|
IssuedAt: time.Now().Unix(),
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
params := url.Values{
|
|
"code": {"1123"},
|
|
"id_token": {idToken},
|
|
"state": {st},
|
|
}
|
|
|
|
w := httptest.NewRecorder()
|
|
r, err := http.NewRequest(http.MethodPost,
|
|
pb.PathForAuthCallback,
|
|
strings.NewReader(params.Encode()))
|
|
r.Header.Add(header.ContentType, "application/x-www-form-urlencoded")
|
|
require.NoError(t, err)
|
|
|
|
h(w, r, nil)
|
|
assert.Equal(t, http.StatusSeeOther, w.Code)
|
|
})
|
|
|
|
t.Run("old_state_post_token_fagment", func(t *testing.T) {
|
|
t.Parallel()
|
|
st, err := service.Protect(ctx, &auth.State{
|
|
Provider: pb.IDP_Google,
|
|
ClientID: "1234",
|
|
Scope: "email",
|
|
Nonce: "8245027",
|
|
DPoPJwk: "1328450198456019",
|
|
Audience: []string{"http://localhost:38989"},
|
|
RedirectURL: "http://localhost:38989",
|
|
ResponseMode: "fragment",
|
|
ResponseType: "token",
|
|
IssuedAt: time.Now().Add(-6 * time.Minute).Unix(),
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
params := url.Values{
|
|
"code": {"1123"},
|
|
"id_token": {idToken},
|
|
"state": {st},
|
|
}
|
|
|
|
w := httptest.NewRecorder()
|
|
r, err := http.NewRequest(http.MethodPost,
|
|
pb.PathForAuthCallback,
|
|
strings.NewReader(params.Encode()))
|
|
r.Header.Add(header.ContentType, "application/x-www-form-urlencoded")
|
|
require.NoError(t, err)
|
|
|
|
h(w, r, nil)
|
|
require.Equal(t, http.StatusSeeOther, w.Code)
|
|
loc, err := url.Parse(w.Header().Get(header.Location))
|
|
require.NoError(t, err)
|
|
vals, err := url.ParseQuery(loc.Fragment)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, url.Values{
|
|
pb.OAuthParamError: {pb.OAuthErrorInvalidRequest},
|
|
pb.OAuthParamErrorDescription: {"invalid state"},
|
|
}, vals)
|
|
})
|
|
}
|
|
|
|
func Test_DPoPCallbackHandlerGoogle(t *testing.T) {
|
|
service := testServer.Service(auth.ServiceName).(*auth.Service)
|
|
require.NotNil(t, service)
|
|
|
|
ctx := context.Background()
|
|
h := service.CallbackHandler()
|
|
|
|
server := servefiles.New(t)
|
|
server.SetBaseDirs("testdata")
|
|
|
|
baseURL := server.URL()
|
|
o := &oauth2client.ClientConfig{
|
|
ProviderID: strings.ToLower(pb.IDP_Google.String()),
|
|
ClientID: "1234",
|
|
ClientSecret: "1234",
|
|
Scopes: []string{"email"},
|
|
RedirectURL: "http://localhost:38989",
|
|
ResponseType: pb.OAuthResponseTypeCode,
|
|
AuthURL: baseURL + "/o/oauth2/v2/auth",
|
|
TokenURL: baseURL + "/token",
|
|
UserinfoURL: baseURL + "/v1/userinfo",
|
|
}
|
|
err := service.SetAuthProvider(ctx, o)
|
|
require.NoError(t, err)
|
|
|
|
t.Run("valid_state_post_token_google", func(t *testing.T) {
|
|
t.Parallel()
|
|
st, err := service.Protect(ctx, &auth.State{
|
|
Provider: pb.IDP_Google,
|
|
ClientID: "1234",
|
|
Scope: "email",
|
|
Nonce: "8245027",
|
|
DPoPJwk: "1328450198456019",
|
|
Audience: []string{"http://localhost:38989"},
|
|
RedirectURL: "http://localhost:38989",
|
|
ResponseMode: "form_post",
|
|
ResponseType: "token",
|
|
IssuedAt: time.Now().Unix(),
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
params := url.Values{
|
|
"code": {"1123"},
|
|
"state": {st},
|
|
}
|
|
|
|
w := httptest.NewRecorder()
|
|
r, err := http.NewRequest(http.MethodPost,
|
|
pb.PathForAuthCallback,
|
|
strings.NewReader(params.Encode()))
|
|
r.Header.Add(header.ContentType, "application/x-www-form-urlencoded")
|
|
require.NoError(t, err)
|
|
|
|
h(w, r, nil)
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
|
assert.Contains(t, w.Body.String(), "<title>Submit This Form</title>")
|
|
})
|
|
}
|
|
|
|
func Test_DPoPCallbackHandlerLocal(t *testing.T) {
|
|
service := testServer.Service(auth.ServiceName).(*auth.Service)
|
|
require.NotNil(t, service)
|
|
|
|
ctx := context.Background()
|
|
h := service.CallbackHandler()
|
|
|
|
server := servefiles.New(t)
|
|
server.SetBaseDirs("testdata")
|
|
|
|
baseURL := server.URL()
|
|
o := &oauth2client.ClientConfig{
|
|
ProviderID: strings.ToLower(pb.IDP_Local.String()),
|
|
ClientID: "1234",
|
|
ClientSecret: "1234",
|
|
Scopes: []string{"email"},
|
|
RedirectURL: "http://localhost:38989",
|
|
ResponseType: pb.OAuthResponseTypeCode,
|
|
AuthURL: baseURL + "/login/oauth/authorize",
|
|
TokenURL: baseURL + "/login/oauth/access_token",
|
|
UserinfoURL: baseURL + "/user",
|
|
}
|
|
err := service.SetAuthProvider(ctx, o)
|
|
require.NoError(t, err)
|
|
|
|
t.Run("valid_state_post_tokenlocal", func(t *testing.T) {
|
|
t.Parallel()
|
|
st, err := service.Protect(ctx, &auth.State{
|
|
Provider: pb.IDP_Local,
|
|
ClientID: "1234",
|
|
Scope: "email",
|
|
Nonce: "8245027",
|
|
DPoPJwk: "1328450198456019",
|
|
Audience: []string{"http://localhost:38989"},
|
|
RedirectURL: "http://localhost:38989",
|
|
ResponseMode: "query",
|
|
ResponseType: "token",
|
|
IssuedAt: time.Now().Unix(),
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
params := url.Values{
|
|
"code": {"1123"},
|
|
"state": {st},
|
|
}
|
|
|
|
w := httptest.NewRecorder()
|
|
r, err := http.NewRequest(http.MethodPost,
|
|
pb.PathForAuthCallback,
|
|
strings.NewReader(params.Encode()))
|
|
r.Header.Add(header.ContentType, "application/x-www-form-urlencoded")
|
|
require.NoError(t, err)
|
|
|
|
h(w, r, nil)
|
|
assert.Equal(t, http.StatusSeeOther, w.Code)
|
|
assert.NotEmpty(t, w.Header().Get(header.Location))
|
|
assert.Empty(t, w.Body.String())
|
|
})
|
|
}
|
|
|
|
func Test_AuthHTTPHandler(t *testing.T) {
|
|
service := testServer.Service(auth.ServiceName).(*auth.Service)
|
|
require.NotNil(t, service)
|
|
|
|
t.Run("providers_rpc", func(t *testing.T) {
|
|
t.Parallel()
|
|
w, r, _ := testutils.CreateRequest(http.MethodPost, pb.Auth_GetProviders_FullMethodName, &emptypb.Empty{}, nil)
|
|
|
|
service.AuthHTTPHandler()(w, r, nil)
|
|
require.Equal(t, http.StatusOK, w.Code)
|
|
|
|
body := w.Body.Bytes()
|
|
var res pb.AuthProvidersResponse
|
|
err := json.Unmarshal(body, &res)
|
|
require.NoError(t, err)
|
|
assert.NotEmpty(t, res.AuthURL)
|
|
assert.NotEmpty(t, res.Providers)
|
|
})
|
|
}
|
|
|
|
func Test_DPoPCallbackHandlerGithub(t *testing.T) {
|
|
service := testServer.Service(auth.ServiceName).(*auth.Service)
|
|
require.NotNil(t, service)
|
|
|
|
ctx := context.Background()
|
|
h := service.CallbackHandler()
|
|
|
|
server := servefiles.New(t)
|
|
server.SetBaseDirs("testdata")
|
|
|
|
baseURL := server.URL()
|
|
o := &oauth2client.ClientConfig{
|
|
ProviderID: strings.ToLower(pb.IDP_Github.String()),
|
|
ClientID: "1234",
|
|
ClientSecret: "1234",
|
|
Scopes: []string{"email"},
|
|
RedirectURL: "http://localhost:38989",
|
|
ResponseType: pb.OAuthResponseTypeCode,
|
|
AuthURL: baseURL + "/login/oauth/authorize",
|
|
TokenURL: baseURL + "/login/oauth/access_token",
|
|
UserinfoURL: baseURL + "/user",
|
|
}
|
|
err := service.SetAuthProvider(ctx, o)
|
|
require.NoError(t, err)
|
|
|
|
u, err := url.Parse(baseURL + "/")
|
|
require.NoError(t, err)
|
|
service.BaseURL = u
|
|
|
|
t.Run("valid_state_post_token_github", func(t *testing.T) {
|
|
st, err := service.Protect(ctx, &auth.State{
|
|
Provider: pb.IDP_Github,
|
|
ClientID: "1234",
|
|
Scope: "email",
|
|
Nonce: "8245027",
|
|
DPoPJwk: "1328450198456019",
|
|
Audience: []string{"http://localhost:38989"},
|
|
RedirectURL: "http://localhost:38989",
|
|
ResponseMode: "form_post",
|
|
ResponseType: "token id_token",
|
|
IssuedAt: time.Now().Unix(),
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
params := url.Values{
|
|
"code": {"1123"},
|
|
"state": {st},
|
|
}
|
|
|
|
w := httptest.NewRecorder()
|
|
r, err := http.NewRequest(http.MethodPost,
|
|
pb.PathForAuthCallback,
|
|
strings.NewReader(params.Encode()))
|
|
r.Header.Add(header.ContentType, "application/x-www-form-urlencoded")
|
|
require.NoError(t, err)
|
|
|
|
h(w, r, nil)
|
|
// the refusal is posted to the client, in its response mode
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
|
body := w.Body.String()
|
|
assert.Contains(t, body, `action="http://localhost:38989"`)
|
|
assert.Contains(t, body, `name="error" value="unsupported_response_type"`)
|
|
assert.Contains(t, body, `name="error_description" value="unsupported response_type: 'token id_token'"`)
|
|
})
|
|
|
|
var token string
|
|
// valid_state_get_token_github
|
|
{
|
|
st, err := service.Protect(ctx, &auth.State{
|
|
Provider: pb.IDP_Github,
|
|
ClientID: "1234",
|
|
Scope: "email",
|
|
Nonce: "8245027",
|
|
DPoPJwk: "1328450198456019",
|
|
Audience: []string{"http://localhost:38989"},
|
|
RedirectURL: "http://localhost:38989",
|
|
ResponseMode: "query",
|
|
ResponseType: "token",
|
|
IssuedAt: time.Now().Unix(),
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
params := url.Values{
|
|
"code": {"1123"},
|
|
"state": {st},
|
|
}
|
|
|
|
w := httptest.NewRecorder()
|
|
r, err := http.NewRequest(http.MethodPost,
|
|
pb.PathForAuthCallback,
|
|
strings.NewReader(params.Encode()))
|
|
r.Header.Add(header.ContentType, "application/x-www-form-urlencoded")
|
|
require.NoError(t, err)
|
|
|
|
h(w, r, nil)
|
|
assert.Equal(t, http.StatusSeeOther, w.Code)
|
|
loc := w.Header().Get("Location")
|
|
require.NotEmpty(t, loc)
|
|
|
|
u, err := url.Parse(loc)
|
|
require.NoError(t, err)
|
|
token = urlutil.GetValue(u.Query(), "token")
|
|
}
|
|
require.NotEmpty(t, token)
|
|
|
|
c := map[string]any{
|
|
"sub": "1234",
|
|
"email": "[email protected]",
|
|
"role": "trustyca-admin",
|
|
// the provider of an older token is the number of the enum value,
|
|
// a float64 once the JSON of the token is decoded
|
|
"provider": float64(pb.IDP_Github),
|
|
}
|
|
adminIdn := identity.NewIdentity("trustyca-admin", "10.0.0.1", "", c, token, "DPoP", identity.MethodDPoP)
|
|
|
|
t.Run("userinfo", func(t *testing.T) {
|
|
w, r, err := testutils.CreateRequest(http.MethodGet, pb.PathForAuthUserinfo, nil, adminIdn)
|
|
require.NoError(t, err)
|
|
r.Header.Add(header.Authorization, header.DPoP+" "+token)
|
|
_, err = dpop.ForRequest(dpopSigner, r, nil)
|
|
require.NoError(t, err)
|
|
|
|
service.UserinfoHandler()(w, r, nil)
|
|
require.Equal(t, http.StatusOK, w.Code)
|
|
require.Equal(t, "application/json", w.Header().Get(header.ContentType))
|
|
|
|
body := w.Body.Bytes()
|
|
claims := map[string]any{}
|
|
err = json.Unmarshal(body, &claims)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "1234", claims["sub"])
|
|
assert.Equal(t, "[email protected]", claims["email"])
|
|
})
|
|
|
|
t.Run("userinfo_legacy", func(t *testing.T) {
|
|
w, r, err := testutils.CreateRequest(http.MethodGet, pb.PathForAuthUserinfo, nil, adminIdn)
|
|
require.NoError(t, err)
|
|
r.Header.Add(header.Authorization, header.DPoP+" "+token)
|
|
_, err = dpop.ForRequest(dpopSigner, r, nil)
|
|
require.NoError(t, err)
|
|
|
|
service.UserTokenHandler()(w, r, nil)
|
|
require.Equal(t, http.StatusOK, w.Code)
|
|
require.Equal(t, "application/json", w.Header().Get(header.ContentType))
|
|
|
|
body := w.Body.Bytes()
|
|
var ti pb.UserTokenResponse
|
|
err = json.Unmarshal(body, &ti)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "1234", ti.UserInfo.ID)
|
|
assert.Equal(t, "[email protected]", ti.UserInfo.Email)
|
|
assert.Equal(t, pb.IDP_Github, ti.Token.Provider)
|
|
})
|
|
|
|
t.Run("usertoken_rpc", func(t *testing.T) {
|
|
w, r, err := testutils.CreateRequest(http.MethodGet, pb.Auth_GetUserToken_FullMethodName, &emptypb.Empty{}, adminIdn)
|
|
require.NoError(t, err)
|
|
r.Header.Add(header.Authorization, header.DPoP+" "+token)
|
|
_, err = dpop.ForRequest(dpopSigner, r, nil)
|
|
require.NoError(t, err)
|
|
|
|
service.AuthHTTPHandler()(w, r, nil)
|
|
require.Equal(t, http.StatusOK, w.Code)
|
|
require.Equal(t, "application/json", w.Header().Get(header.ContentType))
|
|
|
|
body := w.Body.Bytes()
|
|
var ti pb.UserTokenResponse
|
|
err = json.Unmarshal(body, &ti)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "1234", ti.UserInfo.ID)
|
|
assert.Equal(t, "[email protected]", ti.UserInfo.Email)
|
|
assert.Equal(t, pb.IDP_Github, ti.Token.Provider)
|
|
})
|
|
|
|
t.Run("caller_rpc", func(t *testing.T) {
|
|
w, r, _ := testutils.CreateRequest(http.MethodPost, pb.Auth_Caller_FullMethodName, &emptypb.Empty{}, adminIdn)
|
|
r.Header.Add(header.Authorization, header.DPoP+" "+token)
|
|
|
|
_, err = dpop.ForRequest(dpopSigner, r, nil)
|
|
require.NoError(t, err)
|
|
|
|
service.AuthHTTPHandler()(w, r, nil)
|
|
require.Equal(t, http.StatusOK, w.Code)
|
|
|
|
body := w.Body.Bytes()
|
|
var res pb.CallerStatusResponse
|
|
err = json.Unmarshal(body, &res)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "10.0.0.1", res.Subject)
|
|
assert.Equal(t, "trustyca-admin", res.Role)
|
|
})
|
|
}
|
|
|
|
func Test_RequestValidate(t *testing.T) {
|
|
t.Parallel()
|
|
r := auth.Request{
|
|
ResponseType: "rt",
|
|
}
|
|
assert.EqualError(t, r.Validate(), "invalid_request: unsupported response_type: 'rt'")
|
|
r = auth.Request{
|
|
ResponseType: "token",
|
|
}
|
|
assert.EqualError(t, r.Validate(), "invalid_request: missing parameter: redirect_uri")
|
|
r = auth.Request{
|
|
ResponseType: "token",
|
|
RedirectURI: "http://localhost:8880/v1/auth/callback",
|
|
}
|
|
assert.EqualError(t, r.Validate(), "invalid_request: missing parameter: scope")
|
|
r = auth.Request{
|
|
ResponseType: "token",
|
|
RedirectURI: "http://localhost:8880/v1/auth/callback",
|
|
Scope: "email",
|
|
}
|
|
assert.NoError(t, r.Validate())
|
|
r = auth.Request{
|
|
ResponseType: "token",
|
|
RedirectURI: "http://localhost:8880/v1/auth/callback",
|
|
Scope: "email",
|
|
Provider: pb.IDP_Github,
|
|
}
|
|
assert.NoError(t, r.Validate())
|
|
}
|
|
|
|
func Test_TenantHandler(t *testing.T) {
|
|
t.Setenv("TRUSTYCA_GOOGLE_CLIENT_ID", "testclientid")
|
|
t.Setenv("TRUSTYCA_GOOGLE_CLIENT_SECRET", "testclientsecret")
|
|
|
|
service := testServer.Service(auth.ServiceName).(*auth.Service)
|
|
require.NotNil(t, service)
|
|
|
|
ctx := context.Background()
|
|
h := service.CallbackHandler()
|
|
|
|
server := servefiles.New(t)
|
|
server.SetBaseDirs("testdata")
|
|
baseURL := server.URL()
|
|
o := &oauth2client.ClientConfig{
|
|
ProviderID: strings.ToLower(pb.IDP_Google.String()),
|
|
ClientID: "1234",
|
|
ClientSecret: "1234",
|
|
Scopes: []string{"email"},
|
|
RedirectURL: "http://localhost:38989",
|
|
ResponseType: pb.OAuthResponseTypeCode,
|
|
AuthURL: baseURL + "/o/oauth2/v2/auth",
|
|
TokenURL: baseURL + "/token",
|
|
UserinfoURL: baseURL + "/v1/userinfo",
|
|
}
|
|
err := service.SetAuthProvider(ctx, o)
|
|
require.NoError(t, err)
|
|
|
|
idToken, err := service.JwtSigner.Sign(
|
|
ctx,
|
|
jwt.CreateClaims(
|
|
certutil.RandomString(8),
|
|
"[email protected]",
|
|
service.JwtSigner.Issuer(),
|
|
[]string{"http://localhost:38989"},
|
|
60*time.Minute,
|
|
jwt.MapClaims{
|
|
"sub": "[email protected]",
|
|
"picture": "https://bitly.io/2342134",
|
|
"email_verified": true,
|
|
},
|
|
))
|
|
require.NoError(t, err)
|
|
|
|
var accessToken string
|
|
|
|
t.Run("valid_state_post_code_google", func(t *testing.T) {
|
|
st, err := service.Protect(ctx, &auth.State{
|
|
Provider: pb.IDP_Google,
|
|
ClientID: "1234",
|
|
Scope: "email",
|
|
Nonce: "8245027",
|
|
DPoPJwk: "1328450198456019",
|
|
Audience: []string{"http://localhost:38989"},
|
|
RedirectURL: "http://localhost:38989",
|
|
ResponseType: pb.OAuthResponseTypeToken,
|
|
IssuedAt: time.Now().Unix(),
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
params := url.Values{
|
|
"code": {"1123"},
|
|
"id_token": {idToken},
|
|
"state": {st},
|
|
}
|
|
|
|
w := httptest.NewRecorder()
|
|
r, err := http.NewRequest(http.MethodPost,
|
|
pb.PathForAuthCallback,
|
|
strings.NewReader(params.Encode()))
|
|
r.Header.Add(header.ContentType, "application/x-www-form-urlencoded")
|
|
require.NoError(t, err)
|
|
|
|
h(w, r, nil)
|
|
require.Equal(t, http.StatusSeeOther, w.Code, w.Body.String())
|
|
loc := w.Header().Get("Location")
|
|
require.NotEmpty(t, loc)
|
|
|
|
u, err := url.Parse(loc)
|
|
require.NoError(t, err)
|
|
accessToken = urlutil.GetQueryString(u, "token")
|
|
require.NotEmpty(t, accessToken)
|
|
})
|
|
|
|
require.NotEmpty(t, accessToken)
|
|
|
|
inClaims, err := service.JwtParser.ParseToken(ctx, accessToken, nil)
|
|
require.NoError(t, err)
|
|
|
|
var claims jwt.Claims
|
|
err = inClaims.To(&claims)
|
|
require.NoError(t, err)
|
|
assert.NotEmpty(t, claims.Subject)
|
|
|
|
t.Run("usertoken", func(t *testing.T) {
|
|
w := httptest.NewRecorder()
|
|
r, _ := http.NewRequest(http.MethodGet, pb.PathForAuthUserinfo, nil)
|
|
require.NoError(t, err)
|
|
|
|
c := map[string]any{
|
|
"sub": claims.Subject,
|
|
"email": claims.Email,
|
|
}
|
|
r = identity.WithTestIdentity(r, identity.NewIdentity("admin", "10.0.0.1", "", c, accessToken, "Bearer", identity.MethodJWT))
|
|
|
|
service.UserTokenHandler()(w, r, nil)
|
|
require.Equal(t, http.StatusOK, w.Code)
|
|
require.Equal(t, "application/json", w.Header().Get(header.ContentType))
|
|
|
|
body := w.Body.Bytes()
|
|
var ti pb.UserTokenResponse
|
|
err = json.Unmarshal(body, &ti)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, claims.Subject, ti.UserInfo.ID)
|
|
assert.NotEmpty(t, claims.Email, ti.UserInfo.Email)
|
|
})
|
|
}
|
|
|
|
func TestCallerStatusGrpc(t *testing.T) {
|
|
t.Parallel()
|
|
res, err := authClient.Caller(context.Background(), &emptypb.Empty{})
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, identity.GuestRoleName, res.Role)
|
|
}
|
|
|
|
func TestCallerStatusHttp(t *testing.T) {
|
|
t.Parallel()
|
|
w := httptest.NewRecorder()
|
|
client, err := retriable.Default(httpAddr)
|
|
require.NoError(t, err)
|
|
|
|
ctx := retriable.WithHeaders(context.Background(), textContentHeaders)
|
|
|
|
hdr, _, err := client.Get(ctx, pb.PathForStatusCaller, w)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
|
// TODO: add Print
|
|
//assert.Contains(t, hdr.Get(header.ContentType), header.TextPlain)
|
|
assert.Contains(t, hdr.Get(header.ContentType), header.ApplicationJSON)
|
|
}
|
|
|
|
func TestCallerStatusHttpJSON(t *testing.T) {
|
|
t.Parallel()
|
|
res := new(pb.CallerStatusResponse)
|
|
client, err := retriable.Default(httpAddr)
|
|
require.NoError(t, err)
|
|
ctx := retriable.WithHeaders(context.Background(), jsonContentHeaders)
|
|
|
|
hdr, sc, err := client.Get(ctx, pb.PathForStatusCaller, res)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, http.StatusOK, sc)
|
|
assert.Contains(t, hdr.Get(header.ContentType), header.ApplicationJSON)
|
|
assert.NotEmpty(t, res.Role)
|
|
}
|