Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cor 484 scopes n claims #407

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ serve-native:
serve-pwa:
@./scripts/serve-pwa.sh

.PHONY: serve-admin
.PHONY: serve-authnz-frontend
serve-authnz-frontend:
@./scripts/serve-authnz-frontend.sh

Expand Down
2 changes: 2 additions & 0 deletions cmd/devinit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,8 @@ func createIdentityProviders(factory store.Factory, err error, orgId string, env
ClientID: envCfg.idpClientId,
ClientSecret: envCfg.idpClientSecret,
EmailDomain: "nrc.no",
Scopes: "openid profile offline_access",
ClaimMappings: types.ClaimMappings{Mappings: nil, Version: "0"},
}
if len(idps) == 0 {
_, err := idpStore.Create(context.Background(), idp, store.IdentityProviderCreateOptions{})
Expand Down
4 changes: 4 additions & 0 deletions deployments/envoy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ static_resources:
stat_prefix: ingress_http
http_filters:
- name: envoy.filters.http.router
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router
route_config:
name: local_route
virtual_hosts:
Expand Down Expand Up @@ -71,6 +73,8 @@ static_resources:
timeout: 0.250s
path_prefix: /apis/authorization.nrc.no/v1
- name: envoy.filters.http.router
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router
route_config:
name: local_route
virtual_hosts:
Expand Down
3 changes: 1 addition & 2 deletions deployments/webapp.docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ services:
- ../creds/envoy/tls.crt:/var/run/tls.crt
- ../certs/ca.crt:/var/run/ca.crt
network_mode: host
restart: on-failure
command:
- /usr/local/bin/envoy
- -c
- /etc/envoy.yaml
- -l
- debug

oidc:
image: node:16
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { FC, useCallback, useEffect, useMemo } from 'react';
import { FC, useCallback, useEffect, useMemo, useState } from 'react';
import { useForm } from 'react-hook-form';
import classNames from 'classnames';

import { useApiClient, useFormValidation } from '../../../hooks/hooks';
import { Organization } from '../../../types/types';
import { IdentityProvider, Organization } from '../../../types/types';

type Props = {
id?: string;
Expand All @@ -17,6 +17,8 @@ type FormData = {
clientSecret: string;
organizationId: string;
emailDomain: string;
scopes: string;
claimMappings: { Version: string; Mappings: any };
};

export const IdentityProviderEditor: FC<Props> = (props) => {
Expand All @@ -28,6 +30,8 @@ export const IdentityProviderEditor: FC<Props> = (props) => {

const form = useForm<FormData>({ mode: 'onChange' });

const [version, setVersion] = useState<string>('0');

const {
register,
handleSubmit,
Expand All @@ -37,56 +41,84 @@ export const IdentityProviderEditor: FC<Props> = (props) => {

const { fieldErrors, fieldClasses } = useFormValidation(isNew, form);

const setData = (data: IdentityProvider) => {
setValue('name', data.name);
setValue('clientId', data.clientId);
setValue('organizationId', data.organizationId);
setValue('issuer', data.domain);
setValue('emailDomain', data.emailDomain);
setValue('clientSecret', '');
setValue('scopes', data.scopes);
setValue(
'claimMappings.Mappings',
JSON.stringify(data.claimMappings.Mappings),
);
setVersion(data.claimMappings.Version);
};

useEffect(() => {
if (id) {
apiClient.getIdentityProvider({ id }).then((resp) => {
if (resp.response) {
setValue('name', resp.response.name);
setValue('clientId', resp.response.clientId);
setValue('organizationId', resp.response.organizationId);
setValue('issuer', resp.response.domain);
setValue('emailDomain', resp.response.emailDomain);
setValue('clientSecret', '');
console.log('RESP', resp.response);
setData(resp.response);
}
});
}
}, [apiClient, id, setValue]);

const onSubmit = useCallback(
(args: FormData) => {
const obj = {
async (args: FormData) => {
const newVersion = `${parseInt(version, 10) + 1}`;

const object = {
id,
name: args.name,
clientId: args.clientId,
clientSecret: args.clientSecret,
domain: args.issuer,
organizationId: organization.id,
emailDomain: args.emailDomain,
scopes: args.scopes,
claimMappings: {
Version: newVersion,
Mappings: JSON.parse(args.claimMappings.Mappings),
},
};
let resp;
if (id) {
return apiClient.updateIdentityProvider({
object: obj,
resp = await apiClient.updateIdentityProvider({
object,
});
} else {
resp = await apiClient.createIdentityProvider({
object,
});
}
return apiClient.createIdentityProvider({
object: obj,
});
return resp.response && setData(resp.response);
},
[apiClient, id, organization.id],
[apiClient, id, organization.id, version],
);

return (
<div className={classNames('card bg-dark border-secondary')}>
<div className="card-body">
<form className="needs-validation" noValidate onSubmit={handleSubmit(onSubmit)}>
<form
className="needs-validation"
noValidate
onSubmit={handleSubmit(onSubmit)}
>
<div className={classNames('form-group mb-2')}>
<label className="form-label text-light">Name</label>
<input
{...register('name', {
required: true,
pattern: /^[a-zA-Z0-9\-_ ]+$/,
})}
className={classNames('form-control form-control-darkula', fieldClasses('name'))}
className={classNames(
'form-control form-control-darkula',
fieldClasses('name'),
)}
/>
{fieldErrors('name')}
</div>
Expand All @@ -97,7 +129,10 @@ export const IdentityProviderEditor: FC<Props> = (props) => {
required: true,
pattern: /^https?:\/\/[a-zA-Z0-9.\-_]+(:[0-9]+)?$/,
})}
className={classNames('form-control form-control-darkula', fieldClasses('issuer'))}
className={classNames(
'form-control form-control-darkula',
fieldClasses('issuer'),
)}
/>
{fieldErrors('issuer')}
</div>
Expand All @@ -107,7 +142,10 @@ export const IdentityProviderEditor: FC<Props> = (props) => {
{...register('emailDomain', {
required: true,
})}
className={classNames('form-control form-control-darkula', fieldClasses('emailDomain'))}
className={classNames(
'form-control form-control-darkula',
fieldClasses('emailDomain'),
)}
/>
{fieldErrors('emailDomain')}
</div>
Expand All @@ -117,7 +155,10 @@ export const IdentityProviderEditor: FC<Props> = (props) => {
{...register('clientId', {
required: true,
})}
className={classNames('form-control form-control-darkula', fieldClasses('clientId'))}
className={classNames(
'form-control form-control-darkula',
fieldClasses('clientId'),
)}
/>
{fieldErrors('clientId')}
</div>
Expand All @@ -128,11 +169,45 @@ export const IdentityProviderEditor: FC<Props> = (props) => {
{...register('clientSecret', {
required: isNew,
})}
className={classNames('form-control form-control-darkula', fieldClasses('clientSecret'))}
className={classNames(
'form-control form-control-darkula',
fieldClasses('clientSecret'),
)}
placeholder={isNew ? '' : '********'}
/>
{fieldErrors('clientSecret')}
</div>

<div className="form-group mb-2">
<label className="form-label text-light">Scopes</label>
<input
{...register('scopes', {
required: true,
})}
className={classNames(
'form-control form-control-darkula',
fieldClasses('scopes'),
)}
/>
{fieldErrors('scopes')}
</div>

<div className="form-group mb-2">
<label className="form-label text-light">
Claim Mapping (Version: {version})
</label>
<textarea
{...register('claimMappings.Mappings', {
required: true,
})}
className={classNames(
'form-control form-control-darkula',
fieldClasses('claimMappings.Mappings'),
)}
/>
</div>
{fieldErrors('claimMappings')}

<button disabled={isSubmitting} className="btn btn-success mt-2">
{props.id ? 'Update Identity Provider' : 'Create Identity Provider'}
</button>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FC, Fragment } from 'react';
import { FC } from 'react';
import { Link, useRouteMatch } from 'react-router-dom';

import { Organization } from '../../../types/types';
Expand All @@ -23,11 +23,22 @@ export const IdentityProviders: FC<Props> = (props) => {

<div className="list-group list-group-darkula">
{idps.map((idp) => (
<Link className="list-group-item list-group-item-action" to={`${match.url}/${idp.id}`}>
{idp.name} <span className="badge bg-dark font-monospace">{idp.emailDomain}</span>
<Link
key={idp.id}
className="list-group-item list-group-item-action"
to={`${match.url}/${idp.id}`}
>
{idp.name}{' '}
<span className="badge bg-dark font-monospace">
{idp.emailDomain}
</span>
</Link>
))}
{idps.length === 0 ? <div className="disabled list-group-item">No Identity Provider</div> : <></>}
{idps.length === 0 ? (
<div className="disabled list-group-item">No Identity Provider</div>
) : (
<></>
)}
</div>
</div>
);
Expand Down
22 changes: 19 additions & 3 deletions frontend/apps/core-authnz-frontend/src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,19 @@ export interface Client

export type IdentityProviderList = { items: IdentityProvider[] };

export type TokenEndpointAuthMethod = 'client_secret_post' | 'client_secret_basic' | 'private_key_jwt' | 'none';
export type TokenEndpointAuthMethod =
| 'client_secret_post'
| 'client_secret_basic'
| 'private_key_jwt'
| 'none';

export type ResponseType = 'code' | 'token' | 'id_token';

export type GrantType = 'authorization_code' | 'refresh_token' | 'client_credentials' | 'implicit';
export type GrantType =
| 'authorization_code'
| 'refresh_token'
| 'client_credentials'
| 'implicit';

export class OAuth2Client {
public id = '';
Expand All @@ -60,7 +68,8 @@ export class OAuth2Client {

public allowedCorsOrigins: string[] = [];

public tokenEndpointAuthMethod: TokenEndpointAuthMethod = 'client_secret_basic';
public tokenEndpointAuthMethod: TokenEndpointAuthMethod =
'client_secret_basic';
}

export type OAuth2ClientList = {
Expand Down Expand Up @@ -91,6 +100,13 @@ export class IdentityProvider {
public clientSecret = '';

public emailDomain = '';

public scopes = '';

public claimMappings = {
Version: '0',
Mappings: {},
};
}

export type Session = {
Expand Down
9 changes: 9 additions & 0 deletions pkg/api/types/identity_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ type IdentityProvider struct {
// TODO: add unique constraint for email domains
// TODO: add support for multiple email domains for a single IdentityProvider
EmailDomain string `json:"emailDomain"`

Scopes string `json:"scopes"`

ClaimMappings ClaimMappings `json:"claimMappings"`
}

type ClaimMappings struct {
Version string `json:"Version"`
Mappings map[string]string `json:"Mappings"`
}

// IdentityProviderList represents a list of IdentityProvider
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/login/handlers/login/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func getOauthProvider(
RedirectURL: redirectUri,
}
if loginRequest != nil {
oauthConfig.Scopes = loginRequest.RequestedScope
oauthConfig.Scopes = strings.Split(client.Scopes, " ")
}

verifier := oidcProvider.Verifier(&oidc.Config{
Expand Down
Loading