Skip to content
This repository has been archived by the owner on Jul 28, 2021. It is now read-only.

Commit

Permalink
Custom scopes (#58)
Browse files Browse the repository at this point in the history
* make docker image prefix and name configurable in the docker build script

* customizable scopes to request for OidcConfig instead of default "openid profile email"

* update crd

Co-authored-by: Mario Hros <[email protected]>
  • Loading branch information
ishangulhane and Mario Hros authored Mar 10, 2020
1 parent 179fda2 commit 299839c
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 14 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ Depending on whether you're protecting frontend or backend applications, create
| `clientSecretRef` | object | no | A reference secret that is used to authenticate the client. This can be used in place of the `clientSecret`. |
| `clientSecretRef.name` | string |yes | The name of the Kubernetes Secret that contains the `clientSecret`. |
| `clientSecretRef.key` | string | yes | The field within the Kubernetes Secret that contains the `clientSecret`. |
| `scopes` | array[string] | no | The scopes to request (`openid profile email` by default). |
* For backend applications: The OAuth 2.0 Bearer token spec defines a pattern for protecting APIs by using [JSON Web Tokens (JWTs)](https://tools.ietf.org/html/rfc7519.html). Using the following configuration as an example, define a `JwtConfig` CRD that contains the public key resource, which is used to validate token signatures.
Expand Down
11 changes: 10 additions & 1 deletion adapter/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ package client

import (
"errors"
"strings"

"go.uber.org/zap"

"github.com/ibm-cloud-security/app-identity-and-access-adapter/adapter/authserver"
"github.com/ibm-cloud-security/app-identity-and-access-adapter/adapter/pkg/apis/policies/v1"
v1 "github.com/ibm-cloud-security/app-identity-and-access-adapter/adapter/pkg/apis/policies/v1"
)

// Client encapsulates an authn/z client object
type Client interface {
Name() string
ID() string
Secret() string
Scope() string
AuthorizationServer() authserver.AuthorizationServerService
ExchangeGrantCode(code string, redirectURI string) (*authserver.TokenResponse, error)
RefreshToken(refreshToken string) (*authserver.TokenResponse, error)
Expand All @@ -36,6 +38,13 @@ func (c *remoteClient) Secret() string {
return c.ClientSecret
}

func (c *remoteClient) Scope() string {
if len(c.Scopes) == 0 {
return "openid profile email"
}
return strings.Join(c.Scopes, " ")
}

func (c *remoteClient) AuthorizationServer() authserver.AuthorizationServerService {
return c.authServer
}
Expand Down
15 changes: 8 additions & 7 deletions adapter/pkg/apis/policies/v1/oidc_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@ type OidcConfig struct {

// OidcConfigSpec is the spec for a OidcConfig resource
type OidcConfigSpec struct {
ClientName string
AuthMethod string `json:"authMethod"`
ClientID string `json:"clientId"`
DiscoveryURL string `json:"discoveryUrl"`
ClientSecret string `json:"clientSecret"`
ClientName string
AuthMethod string `json:"authMethod"`
ClientID string `json:"clientId"`
DiscoveryURL string `json:"discoveryUrl"`
ClientSecret string `json:"clientSecret"`
ClientSecretRef ClientSecretRef `json:"clientSecretRef"`
Scopes []string `json:"scopes"`
}

type ClientSecretRef struct {
Name string `json:"name"`
Key string `json:"key"`
Name string `json:"name"`
Key string `json:"key"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down
4 changes: 2 additions & 2 deletions adapter/strategy/web/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"go.uber.org/zap"

"github.com/ibm-cloud-security/app-identity-and-access-adapter/adapter/client"
"github.com/ibm-cloud-security/app-identity-and-access-adapter/config/template"
authnz "github.com/ibm-cloud-security/app-identity-and-access-adapter/config/template"
)

const (
Expand Down Expand Up @@ -60,7 +60,7 @@ func generateAuthorizationURL(c client.Client, redirectURI string, state string)
"client_id": {c.ID()},
"response_type": {"code"},
"redirect_uri": {redirectURI},
"scope": {"openid profile email"},
"scope": {c.Scope()},
"state": {state},
}

Expand Down
6 changes: 3 additions & 3 deletions bin/docker_build_tag_push.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ function buildAndDeploy() {
}


IMAGE_REGISTRY_NAMESPACE=ibmcloudsecurity
APP_NAME=app-identity-and-access-adapter
IMAGE_REGISTRY_NAMESPACE=${IMAGE_REGISTRY_NAMESPACE:-ibmcloudsecurity}
APP_NAME=${APP_NAME:-app-identity-and-access-adapter}
TAG=$(buildTag $1)
IMAGE_TAG=${IMAGE_REGISTRY_NAMESPACE}/${APP_NAME}:${TAG}
sourceDir="$(dirname "${BASH_SOURCE[0]}")"

# Execute
checkTools
buildAndDeploy
export IMAGE_TEST_TAG=${TAG}
export IMAGE_TEST_TAG=${TAG}
5 changes: 5 additions & 0 deletions helm/appidentityandaccessadapter/templates/oidc-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,8 @@ spec:
required:
- name
- key
scopes:
type: array
items:
type: string
minItems: 1
9 changes: 8 additions & 1 deletion tests/fake/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package fake

import "github.com/ibm-cloud-security/app-identity-and-access-adapter/adapter/authserver"
import (
"github.com/ibm-cloud-security/app-identity-and-access-adapter/adapter/authserver"
)

type TokenResponse struct {
Res *authserver.TokenResponse
Expand All @@ -13,6 +15,7 @@ type Client struct {
ClientName string
ClientID string
ClientSecret string
Scopes []string
}

func NewClient(tokenResponse *TokenResponse) *Client {
Expand All @@ -37,6 +40,10 @@ func (m *Client) Secret() string {
return m.ClientSecret
}

func (m *Client) Scope() string {
return "openid profile email"
}

func (m *Client) AuthorizationServer() authserver.AuthorizationServerService {
return m.Server
}
Expand Down

0 comments on commit 299839c

Please sign in to comment.