2021-05-04 22:06:05 +00:00
|
|
|
package oidc
|
|
|
|
|
2022-04-12 03:02:12 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/authelia/authelia/v4/internal/authorization"
|
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
|
|
|
)
|
2021-05-04 22:06:05 +00:00
|
|
|
|
|
|
|
func TestOpenIDConnectStore_GetClientPolicy(t *testing.T) {
|
2021-11-23 09:45:38 +00:00
|
|
|
s := NewOpenIDConnectStore(&schema.OpenIDConnectConfiguration{
|
2022-10-02 02:07:40 +00:00
|
|
|
IssuerCertificateChain: schema.X509CertificateChain{},
|
|
|
|
IssuerPrivateKey: mustParseRSAPrivateKey(exampleIssuerPrivateKey),
|
2021-05-04 22:06:05 +00:00
|
|
|
Clients: []schema.OpenIDConnectClientConfiguration{
|
|
|
|
{
|
|
|
|
ID: "myclient",
|
|
|
|
Description: "myclient desc",
|
|
|
|
Policy: "one_factor",
|
2022-10-20 02:16:36 +00:00
|
|
|
Scopes: []string{ScopeOpenID, ScopeProfile},
|
2021-05-04 22:06:05 +00:00
|
|
|
Secret: "mysecret",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "myotherclient",
|
|
|
|
Description: "myclient desc",
|
|
|
|
Policy: "two_factor",
|
2022-10-20 02:16:36 +00:00
|
|
|
Scopes: []string{ScopeOpenID, ScopeProfile},
|
2021-05-04 22:06:05 +00:00
|
|
|
Secret: "mysecret",
|
|
|
|
},
|
|
|
|
},
|
2022-04-12 03:02:12 +00:00
|
|
|
}, nil)
|
2021-05-04 22:06:05 +00:00
|
|
|
|
|
|
|
policyOne := s.GetClientPolicy("myclient")
|
|
|
|
assert.Equal(t, authorization.OneFactor, policyOne)
|
|
|
|
|
|
|
|
policyTwo := s.GetClientPolicy("myotherclient")
|
|
|
|
assert.Equal(t, authorization.TwoFactor, policyTwo)
|
|
|
|
|
|
|
|
policyInvalid := s.GetClientPolicy("invalidclient")
|
|
|
|
assert.Equal(t, authorization.TwoFactor, policyInvalid)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOpenIDConnectStore_GetInternalClient(t *testing.T) {
|
2021-11-23 09:45:38 +00:00
|
|
|
s := NewOpenIDConnectStore(&schema.OpenIDConnectConfiguration{
|
2022-10-02 02:07:40 +00:00
|
|
|
IssuerCertificateChain: schema.X509CertificateChain{},
|
|
|
|
IssuerPrivateKey: mustParseRSAPrivateKey(exampleIssuerPrivateKey),
|
2021-05-04 22:06:05 +00:00
|
|
|
Clients: []schema.OpenIDConnectClientConfiguration{
|
|
|
|
{
|
|
|
|
ID: "myclient",
|
|
|
|
Description: "myclient desc",
|
|
|
|
Policy: "one_factor",
|
2022-10-20 02:16:36 +00:00
|
|
|
Scopes: []string{ScopeOpenID, ScopeProfile},
|
2021-05-04 22:06:05 +00:00
|
|
|
Secret: "mysecret",
|
|
|
|
},
|
|
|
|
},
|
2022-04-12 03:02:12 +00:00
|
|
|
}, nil)
|
2021-05-04 22:06:05 +00:00
|
|
|
|
|
|
|
client, err := s.GetClient(context.Background(), "myinvalidclient")
|
|
|
|
assert.EqualError(t, err, "not_found")
|
|
|
|
assert.Nil(t, client)
|
|
|
|
|
|
|
|
client, err = s.GetClient(context.Background(), "myclient")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, client)
|
|
|
|
assert.Equal(t, "myclient", client.GetID())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOpenIDConnectStore_GetInternalClient_ValidClient(t *testing.T) {
|
|
|
|
c1 := schema.OpenIDConnectClientConfiguration{
|
|
|
|
ID: "myclient",
|
|
|
|
Description: "myclient desc",
|
|
|
|
Policy: "one_factor",
|
2022-10-20 02:16:36 +00:00
|
|
|
Scopes: []string{ScopeOpenID, ScopeProfile},
|
2021-05-04 22:06:05 +00:00
|
|
|
Secret: "mysecret",
|
|
|
|
}
|
2021-11-23 09:45:38 +00:00
|
|
|
|
|
|
|
s := NewOpenIDConnectStore(&schema.OpenIDConnectConfiguration{
|
2022-10-02 02:07:40 +00:00
|
|
|
IssuerCertificateChain: schema.X509CertificateChain{},
|
|
|
|
IssuerPrivateKey: mustParseRSAPrivateKey(exampleIssuerPrivateKey),
|
|
|
|
Clients: []schema.OpenIDConnectClientConfiguration{c1},
|
2022-04-12 03:02:12 +00:00
|
|
|
}, nil)
|
2021-05-04 22:06:05 +00:00
|
|
|
|
2022-04-07 05:33:53 +00:00
|
|
|
client, err := s.GetFullClient(c1.ID)
|
2021-05-04 22:06:05 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, client)
|
|
|
|
assert.Equal(t, client.ID, c1.ID)
|
|
|
|
assert.Equal(t, client.Description, c1.Description)
|
|
|
|
assert.Equal(t, client.Scopes, c1.Scopes)
|
|
|
|
assert.Equal(t, client.GrantTypes, c1.GrantTypes)
|
|
|
|
assert.Equal(t, client.ResponseTypes, c1.ResponseTypes)
|
|
|
|
assert.Equal(t, client.RedirectURIs, c1.RedirectURIs)
|
|
|
|
assert.Equal(t, client.Policy, authorization.OneFactor)
|
|
|
|
assert.Equal(t, client.Secret, []byte(c1.Secret))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOpenIDConnectStore_GetInternalClient_InvalidClient(t *testing.T) {
|
|
|
|
c1 := schema.OpenIDConnectClientConfiguration{
|
|
|
|
ID: "myclient",
|
|
|
|
Description: "myclient desc",
|
|
|
|
Policy: "one_factor",
|
2022-10-20 02:16:36 +00:00
|
|
|
Scopes: []string{ScopeOpenID, ScopeProfile},
|
2021-05-04 22:06:05 +00:00
|
|
|
Secret: "mysecret",
|
|
|
|
}
|
2021-11-23 09:45:38 +00:00
|
|
|
|
|
|
|
s := NewOpenIDConnectStore(&schema.OpenIDConnectConfiguration{
|
2022-10-02 02:07:40 +00:00
|
|
|
IssuerCertificateChain: schema.X509CertificateChain{},
|
|
|
|
IssuerPrivateKey: mustParseRSAPrivateKey(exampleIssuerPrivateKey),
|
|
|
|
Clients: []schema.OpenIDConnectClientConfiguration{c1},
|
2022-04-12 03:02:12 +00:00
|
|
|
}, nil)
|
2021-05-04 22:06:05 +00:00
|
|
|
|
2022-04-07 05:33:53 +00:00
|
|
|
client, err := s.GetFullClient("another-client")
|
2021-05-04 22:06:05 +00:00
|
|
|
assert.Nil(t, client)
|
|
|
|
assert.EqualError(t, err, "not_found")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOpenIDConnectStore_IsValidClientID(t *testing.T) {
|
2021-11-23 09:45:38 +00:00
|
|
|
s := NewOpenIDConnectStore(&schema.OpenIDConnectConfiguration{
|
2022-10-02 02:07:40 +00:00
|
|
|
IssuerCertificateChain: schema.X509CertificateChain{},
|
|
|
|
IssuerPrivateKey: mustParseRSAPrivateKey(exampleIssuerPrivateKey),
|
2021-05-04 22:06:05 +00:00
|
|
|
Clients: []schema.OpenIDConnectClientConfiguration{
|
|
|
|
{
|
|
|
|
ID: "myclient",
|
|
|
|
Description: "myclient desc",
|
|
|
|
Policy: "one_factor",
|
2022-10-20 02:16:36 +00:00
|
|
|
Scopes: []string{ScopeOpenID, ScopeProfile},
|
2021-05-04 22:06:05 +00:00
|
|
|
Secret: "mysecret",
|
|
|
|
},
|
|
|
|
},
|
2022-04-12 03:02:12 +00:00
|
|
|
}, nil)
|
2021-05-04 22:06:05 +00:00
|
|
|
|
|
|
|
validClient := s.IsValidClientID("myclient")
|
|
|
|
invalidClient := s.IsValidClientID("myinvalidclient")
|
|
|
|
|
|
|
|
assert.True(t, validClient)
|
|
|
|
assert.False(t, invalidClient)
|
2022-04-12 03:02:12 +00:00
|
|
|
}
|