Skip to content

Commit

Permalink
Response code (#4)
Browse files Browse the repository at this point in the history
* add response code as response

* update respcode in client

* remove unnecesary test

* remove unnecesary test

* update create client scope

* refactor response code and create client API
  • Loading branch information
sourabhmandal committed Jun 27, 2023
1 parent 6e5e01c commit f92c598
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 51 deletions.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func (g *GoKeycloak) CreateComponent(ctx context.Context, token, realm string, c
}

// CreateClient creates the given g.
func (g *GoKeycloak) CreateClient(ctx context.Context, clientInitialAccessToken, realm string, newClient Client) (int, CreateClientResponse, error) {
func (g *GoKeycloak) CreateClient(ctx context.Context, clientInitialAccessToken, realm string, newClient CreateClientRequest) (int, CreateClientResponse, error) {
const errMessage = "could not create client"

var result CreateClientResponse
Expand Down
15 changes: 15 additions & 0 deletions client.model.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,19 @@ type CreateClientResponse struct {
BackchannelLogoutSessionRequired bool `json:"backchannel_logout_session_required,omitempty"`
RequirePushedAuthorizationRequests bool `json:"require_pushed_authorization_requests,omitempty"`
FrontchannelLogoutSessionRequired bool `json:"frontchannel_logout_session_required,omitempty"`
}

type CreateClientRequest struct {
RedirectUris []string `json:"redirect_uris,omitempty"`
TokenEndpointAuthMethod string `json:"token_endpoint_auth_method,omitempty"`
GrantTypes []string `json:"grant_types,omitempty"`
ResponseTypes []string `json:"response_types,omitempty"`
Scope string `json:"scope,omitempty"`
SubjectType string `json:"subject_type,omitempty"`
RequestUris []any `json:"request_uris,omitempty"`
TLSClientCertificateBoundAccessTokens bool `json:"tls_client_certificate_bound_access_tokens,omitempty"`
ClientSecretExpiresAt int `json:"client_secret_expires_at,omitempty"`
BackchannelLogoutSessionRequired bool `json:"backchannel_logout_session_required,omitempty"`
RequirePushedAuthorizationRequests bool `json:"require_pushed_authorization_requests,omitempty"`
FrontchannelLogoutSessionRequired bool `json:"frontchannel_logout_session_required,omitempty"`
}
File renamed without changes.
68 changes: 19 additions & 49 deletions test/client_test.go → client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"crypto/rsa"
"encoding/base64"
"fmt"
"io"
"math/rand"
Expand Down Expand Up @@ -321,12 +320,10 @@ func CreatePermission(t *testing.T, client *gokeycloak.GoKeycloak, idOfClient st
return tearDown, *createdPermission.ID
}

func CreateClient(t *testing.T, client *gokeycloak.GoKeycloak, newClient *gokeycloak.Client) (func(), string) {
func CreateClient(t *testing.T, client *gokeycloak.GoKeycloak, newClient *gokeycloak.CreateClientRequest) (func(), string) {
if newClient == nil {
newClient = &gokeycloak.Client{
ClientID: GetRandomNameP("ClientID"),
Name: GetRandomNameP("Name"),
BaseURL: gokeycloak.StringP("http://example.com"),
newClient = &gokeycloak.CreateClientRequest{
RedirectUris: []string{"http://127.0.0.1/"},
}
}
cfg := GetConfig(t)
Expand Down Expand Up @@ -782,32 +779,20 @@ func Test_LoginSignedJWT(t *testing.T) {
}()
pfxData, err := io.ReadAll(f)
require.NoError(t, err)
pKey, cert, err := pkcs12.Decode(pfxData, "secret")
pKey, _, err := pkcs12.Decode(pfxData, "secret")
require.NoError(t, err)
rsaKey, ok := pKey.(*rsa.PrivateKey)
require.True(t, ok)

client := NewClientWithDebug(t)
testClient := gokeycloak.Client{
ID: GetRandomNameP("client-id-"),
ClientID: GetRandomNameP("client-signed-jwt-client-id-"),
ClientAuthenticatorType: gokeycloak.StringP("client-jwt"),
RedirectURIs: &[]string{"localhost"},
StandardFlowEnabled: gokeycloak.BoolP(true),
ServiceAccountsEnabled: gokeycloak.BoolP(true),
Enabled: gokeycloak.BoolP(true),
FullScopeAllowed: gokeycloak.BoolP(true),
Protocol: gokeycloak.StringP("openid-connect"),
PublicClient: gokeycloak.BoolP(false),
Attributes: &map[string]string{
"jwt.credential.certificate": base64.StdEncoding.EncodeToString(cert.Raw),
},
testClient := gokeycloak.CreateClientRequest{
RedirectUris: []string{"localhost"},
}
tearDown, _ := CreateClient(t, client, &testClient)
tearDown, testClientID := CreateClient(t, client, &testClient)
defer tearDown()
_, _, err = client.LoginClientSignedJWT(
context.Background(),
*testClient.ClientID,
testClientID,
cfg.GoKeycloak.Realm,
rsaKey,
jwt.SigningMethodRS256,
Expand Down Expand Up @@ -1458,10 +1443,8 @@ func Test_ClientScopeMappingsClientRoles(t *testing.T) {
cfg := GetConfig(t)
client := NewClientWithDebug(t)
token := GetAdminToken(t, client)
testClient := gokeycloak.Client{
ClientID: GetRandomNameP("ClientID"),
BaseURL: gokeycloak.StringP("https://example.com"),
FullScopeAllowed: gokeycloak.BoolP(false),
testClient := gokeycloak.CreateClientRequest{
RedirectUris: []string{"http://localhost:8080"},
}
// Creating client
tearDownClient, idOfClient := CreateClient(t, client, &testClient)
Expand Down Expand Up @@ -1536,10 +1519,8 @@ func Test_ClientScopeMappingsRealmRoles(t *testing.T) {
cfg := GetConfig(t)
client := NewClientWithDebug(t)
token := GetAdminToken(t, client)
testClient := gokeycloak.Client{
ClientID: GetRandomNameP("ClientID"),
BaseURL: gokeycloak.StringP("http://example.com"),
FullScopeAllowed: gokeycloak.BoolP(false),
testClient := gokeycloak.CreateClientRequest{
RedirectUris: []string{"http://localhost:8080"},
}
// Creating client
tearDownClient, idOfClient := CreateClient(t, client, &testClient)
Expand Down Expand Up @@ -1718,9 +1699,8 @@ func Test_CreateListGetUpdateDeleteClient(t *testing.T) {
client := NewClientWithDebug(t)
token := GetAdminToken(t, client)
clientID := GetRandomNameP("ClientID")
testClient := gokeycloak.Client{
ClientID: clientID,
BaseURL: gokeycloak.StringP("http://example.com"),
testClient := gokeycloak.CreateClientRequest{
RedirectUris: []string{"http://localhost:8080"},
}
t.Logf("Client ID: %s", *clientID)

Expand Down Expand Up @@ -3421,22 +3401,13 @@ func Test_ClientSecret(t *testing.T) {
client := NewClientWithDebug(t)
token := GetAdminToken(t, client)

testClient := gokeycloak.Client{
ID: GetRandomNameP("gocloak-client-id-"),
ClientID: GetRandomNameP("gocloak-client-secret-client-id-"),
Secret: gokeycloak.StringP("initial-secret-key"),
ServiceAccountsEnabled: gokeycloak.BoolP(true),
StandardFlowEnabled: gokeycloak.BoolP(true),
Enabled: gokeycloak.BoolP(true),
FullScopeAllowed: gokeycloak.BoolP(true),
Protocol: gokeycloak.StringP("openid-connect"),
RedirectURIs: &[]string{"localhost"},
ClientAuthenticatorType: gokeycloak.StringP("client-secret"),
testClient := gokeycloak.CreateClientRequest{
RedirectUris: []string{"http://localhost:8080"},
}

tearDown, idOfClient := CreateClient(t, client, &testClient)
defer tearDown()
require.Equal(t, *testClient.ID, idOfClient)
// require.Equal(t, *testClient.ID, idOfClient)

// Keycloak does not support setting the secret while creating the client
_, _, err := client.GetClientSecret(
Expand Down Expand Up @@ -6041,9 +6012,8 @@ func Test_GetClientsWithPagination(t *testing.T) {
token := GetAdminToken(t, client)
clientID := GetRandomNameP("ClientID")

testClient := gokeycloak.Client{
ClientID: clientID,
BaseURL: gokeycloak.StringP("http://example.com"),
testClient := gokeycloak.CreateClientRequest{
RedirectUris: []string{"http://localhost:8080"},
}
t.Logf("Client ID: %s", *clientID)

Expand Down
2 changes: 1 addition & 1 deletion test/oidc_test.go → oidc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func Test_GetUserInfo(t *testing.T) {
context.Background(),
token.AccessToken,
cfg.GoKeycloak.Realm)
require.Error(t, err, "")
require.Error(t, err, nil)
}

func Test_GetRawUserInfo(t *testing.T) {
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit f92c598

Please sign in to comment.