Skip to content

Commit

Permalink
Debug
Browse files Browse the repository at this point in the history
  • Loading branch information
harture committed Apr 15, 2019
1 parent 10e3eb7 commit 17dc1de
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 22 deletions.
2 changes: 2 additions & 0 deletions cmd/keycloakb/keycloak_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@ func main() {
// Management
var managementSubroute = route.PathPrefix("/management").Subrouter()

var getRealmsHandler = ConfigureManagementHandler(ComponentName, ComponentID, idGenerator, keycloakClient, tracer, logger)(managementEndpoints.GetRealms)
var getRealmHandler = ConfigureManagementHandler(ComponentName, ComponentID, idGenerator, keycloakClient, tracer, logger)(managementEndpoints.GetRealm)

var getClientsHandler = ConfigureManagementHandler(ComponentName, ComponentID, idGenerator, keycloakClient, tracer, logger)(managementEndpoints.GetClients)
Expand Down Expand Up @@ -627,6 +628,7 @@ func main() {
var deleteCredentialsForUserHandler = ConfigureManagementHandler(ComponentName, ComponentID, idGenerator, keycloakClient, tracer, logger)(managementEndpoints.DeleteCredentialsForUser)

//realms
managementSubroute.Path("/realms").Methods("GET").Handler(getRealmsHandler)
managementSubroute.Path("/realms/{realm}").Methods("GET").Handler(getRealmHandler)

//clients
Expand Down
9 changes: 1 addition & 8 deletions pkg/event/mock/component.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions pkg/event/mock/instrumenting.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions pkg/event/mock/logging.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions pkg/event/mock/module.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions pkg/event/mock/tracing.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions pkg/event/mock/tracking.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pkg/management/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
)

type KeycloakClient interface {
GetRealms(accessToken string) ([]kc.RealmRepresentation, error)
GetRealm(accessToken string, realmName string) (kc.RealmRepresentation, error)
GetClient(accessToken string, realmName, idClient string) (kc.ClientRepresentation, error)
GetClients(accessToken string, realmName string, paramKV ...string) ([]kc.ClientRepresentation, error)
Expand All @@ -31,6 +32,7 @@ type KeycloakClient interface {

// Component is the event component interface.
type Component interface {
//GetRealms(ctx context.Context) ([]api.RealmRepresentation, error)
GetRealm(ctx context.Context, realmName string) (api.RealmRepresentation, error)
GetClient(ctx context.Context, realmName, idClient string) (api.ClientRepresentation, error)
GetClients(ctx context.Context, realmName string) ([]api.ClientRepresentation, error)
Expand Down
9 changes: 9 additions & 0 deletions pkg/management/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

// Endpoints wraps a service behind a set of endpoints.
type Endpoints struct {
GetRealms endpoint.Endpoint
GetRealm endpoint.Endpoint
GetClient endpoint.Endpoint
GetClients endpoint.Endpoint
Expand All @@ -35,6 +36,7 @@ type Endpoints struct {

// ManagementComponent is the interface of the component to send a query to Keycloak.
type ManagementComponent interface {
//GetRealms(ctx context.Context) ([]api.RealmRepresentation, error)
GetRealm(ctx context.Context, realmName string) (api.RealmRepresentation, error)
GetClient(ctx context.Context, realmName, idClient string) (api.ClientRepresentation, error)
GetClients(ctx context.Context, realmName string) ([]api.ClientRepresentation, error)
Expand All @@ -57,6 +59,13 @@ type ManagementComponent interface {
CreateClientRole(ctx context.Context, realmName, clientID string, role api.RoleRepresentation) (string, error)
}

// MakeRealmsEndpoint makes the Realms endpoint to retrieve all available realms.
// func MakeGetRealmsEndpoint(managementComponent ManagementComponent) endpoint.Endpoint {
// return func(ctx context.Context, req interface{}) (interface{}, error) {
// return managementComponent.GetRealms(ctx)
// }
// }

// MakeRealmEndpoint makes the Realm endpoint to retrieve a realm.
func MakeGetRealmEndpoint(managementComponent ManagementComponent) endpoint.Endpoint {
return func(ctx context.Context, req interface{}) (interface{}, error) {
Expand Down
17 changes: 17 additions & 0 deletions pkg/management/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@ import (
"github.com/stretchr/testify/assert"
)

func TestGetRealmsEndpoint(t *testing.T) {
var mockCtrl = gomock.NewController(t)
defer mockCtrl.Finish()

var mockManagementComponent = mock.NewManagementComponent(mockCtrl)

var e = MakeGetRealmsEndpoint(mockManagementComponent)

var ctx = context.Background()
var req = make(map[string]string)

mockManagementComponent.EXPECT().GetRealms(ctx).Return([]api.RealmRepresentation{}, nil).Times(1)
var res, err = e(ctx, req)
assert.Nil(t, err)
assert.NotNil(t, res)
}

func TestGetRealmEndpoint(t *testing.T) {
var mockCtrl = gomock.NewController(t)
defer mockCtrl.Finish()
Expand Down
13 changes: 13 additions & 0 deletions pkg/management/mock/component.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 23 additions & 4 deletions pkg/middleware/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ func MakeHTTPOIDCTokenValidationMW(keycloakClient KeycloakClient, logger log.Log
}

var splitToken = strings.Split(authorizationHeader, "Bearer ")

if len(splitToken) < 2 {
splitToken = strings.Split(authorizationHeader, "bearer ")
}

var accessToken = splitToken[1]

payload, _, err := jwt.Parse(accessToken)
Expand All @@ -61,7 +66,7 @@ func MakeHTTPOIDCTokenValidationMW(keycloakClient KeycloakClient, logger log.Log
}

var username = jot.Username
var issuer = jot.JWT.Issuer
var issuer = jot.Issuer
var splitIssuer = strings.Split(issuer, "/auth/realms/")
var realm = splitIssuer[1]

Expand All @@ -82,8 +87,22 @@ func MakeHTTPOIDCTokenValidationMW(keycloakClient KeycloakClient, logger log.Log

// Token is JWT token and the custom fields present in OIDC Token provided by Keycloak.
type Token struct {
*jwt.JWT
Username string `json:"preferred_username,omitempty"`
hdr *header
Issuer string `json:"iss,omitempty"`
Subject string `json:"sub,omitempty"`
Audience []string `json:"aud,omitempty"`
ExpirationTime int64 `json:"exp,omitempty"`
NotBefore int64 `json:"nbf,omitempty"`
IssuedAt int64 `json:"iat,omitempty"`
ID string `json:"jti,omitempty"`
Username string `json:"preferred_username,omitempty"`
}

type header struct {
Algorithm string `json:"alg,omitempty"`
KeyID string `json:"kid,omitempty"`
Type string `json:"typ,omitempty"`
ContentType string `json:"cty,omitempty"`
}

// MakeEndpointTokenForRealmMW makes a Endpoint middleware responsible to ensure
Expand All @@ -99,7 +118,7 @@ func MakeEndpointTokenForRealmMW(logger log.Logger) endpoint.Middleware {
// Extract the target realm of the request
var m = req.(map[string]string)
var realmRequested = m["realm"]

// Assert both realms match
if realmAuthorized != realmRequested {
//TODO create a specific error to map it on 403
Expand Down
13 changes: 13 additions & 0 deletions pkg/middleware/mock/management_component.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 17dc1de

Please sign in to comment.