2023-03-15 16:00:52 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
2021-11-10 21:19:40 +00:00
|
|
|
import { create } from 'ember-cli-page-object';
|
|
|
|
import { module, test } from 'qunit';
|
|
|
|
import { setupApplicationTest } from 'ember-qunit';
|
2023-03-22 18:19:11 +00:00
|
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
|
|
|
2021-11-10 21:19:40 +00:00
|
|
|
import authPage from 'vault/tests/pages/auth';
|
|
|
|
import logout from 'vault/tests/pages/logout';
|
|
|
|
import authForm from 'vault/tests/pages/components/auth-form';
|
|
|
|
import enablePage from 'vault/tests/pages/settings/auth/enable';
|
|
|
|
import consoleClass from 'vault/tests/pages/components/console/ui-panel';
|
2022-02-24 20:04:40 +00:00
|
|
|
import { visit, settled, currentURL } from '@ember/test-helpers';
|
2022-09-09 01:06:05 +00:00
|
|
|
import { clearRecord } from 'vault/tests/helpers/oidc-config';
|
2021-11-10 21:19:40 +00:00
|
|
|
const consoleComponent = create(consoleClass);
|
|
|
|
const authFormComponent = create(authForm);
|
|
|
|
|
|
|
|
const OIDC_USER = 'end-user';
|
|
|
|
const USER_PASSWORD = 'mypassword';
|
|
|
|
const OIDC_POLICY = `path "identity/oidc/provider/+/userinfo" {
|
|
|
|
capabilities = ["read", "update"]
|
|
|
|
}`;
|
|
|
|
const USER_TOKEN_TEMPLATE = `{
|
|
|
|
"username": {{identity.entity.aliases.$USERPASS_ACCESSOR.name}},
|
|
|
|
"contact": {
|
|
|
|
"email": {{identity.entity.metadata.email}},
|
|
|
|
"phone_number": {{identity.entity.metadata.phone_number}}
|
|
|
|
}
|
|
|
|
}`;
|
|
|
|
const GROUP_TOKEN_TEMPLATE = `{
|
|
|
|
"groups": {{identity.entity.groups.names}}
|
|
|
|
}`;
|
2021-12-17 03:44:29 +00:00
|
|
|
const oidcEntity = async function (name, policy) {
|
2021-11-10 21:19:40 +00:00
|
|
|
await consoleComponent.runCommands([
|
2023-03-22 18:19:11 +00:00
|
|
|
`write sys/policies/acl/${name} policy=${window.btoa(policy)}`,
|
2021-11-10 21:19:40 +00:00
|
|
|
`write identity/entity name="${OIDC_USER}" policies="${name}" metadata="email=vault@hashicorp.com" metadata="phone_number=123-456-7890"`,
|
|
|
|
`read -field=id identity/entity/name/${OIDC_USER}`,
|
|
|
|
]);
|
|
|
|
return consoleComponent.lastLogOutput;
|
|
|
|
};
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
const oidcGroup = async function (entityId) {
|
2021-11-10 21:19:40 +00:00
|
|
|
await consoleComponent.runCommands([
|
|
|
|
`write identity/group name="engineering" member_entity_ids="${entityId}"`,
|
|
|
|
`read -field=id identity/group/name/engineering`,
|
|
|
|
]);
|
|
|
|
return consoleComponent.lastLogOutput;
|
|
|
|
};
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
const authAccessor = async function (path = 'userpass') {
|
2021-11-10 21:19:40 +00:00
|
|
|
await enablePage.enable('userpass', path);
|
|
|
|
await consoleComponent.runCommands([
|
|
|
|
`write auth/${path}/users/end-user password="${USER_PASSWORD}"`,
|
|
|
|
`read -field=accessor sys/internal/ui/mounts/auth/${path}`,
|
|
|
|
]);
|
|
|
|
return consoleComponent.lastLogOutput;
|
|
|
|
};
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
const entityAlias = async function (entityId, accessor, groupId) {
|
2021-11-10 21:19:40 +00:00
|
|
|
const userTokenTemplate = btoa(USER_TOKEN_TEMPLATE);
|
|
|
|
const groupTokenTemplate = btoa(GROUP_TOKEN_TEMPLATE);
|
|
|
|
|
|
|
|
await consoleComponent.runCommands([
|
|
|
|
`write identity/entity-alias name="end-user" canonical_id="${entityId}" mount_accessor="${accessor}"`,
|
|
|
|
`write identity/oidc/key/sigkey allowed_client_ids="*"`,
|
|
|
|
`write identity/oidc/assignment/my-assignment group_ids="${groupId}" entity_ids="${entityId}"`,
|
|
|
|
`write identity/oidc/scope/user description="scope for user metadata" template="${userTokenTemplate}"`,
|
|
|
|
`write identity/oidc/scope/groups description="scope for groups" template="${groupTokenTemplate}"`,
|
|
|
|
]);
|
|
|
|
return consoleComponent.lastLogOutput.includes('Success');
|
|
|
|
};
|
2021-12-17 03:44:29 +00:00
|
|
|
const setupWebapp = async function (redirect) {
|
2022-11-09 23:15:31 +00:00
|
|
|
const webappName = 'my-webapp';
|
2021-11-10 21:19:40 +00:00
|
|
|
await consoleComponent.runCommands([
|
|
|
|
`write identity/oidc/client/${webappName} redirect_uris="${redirect}" assignments="my-assignment" key="sigkey" id_token_ttl="30m" access_token_ttl="1h"`,
|
|
|
|
`read -field=client_id identity/oidc/client/${webappName}`,
|
|
|
|
]);
|
2022-11-09 23:15:31 +00:00
|
|
|
const output = consoleComponent.lastLogOutput;
|
2021-11-10 21:19:40 +00:00
|
|
|
if (output.includes('error occurred')) {
|
|
|
|
throw new Error(`OIDC setup failed: ${output}`);
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
};
|
2021-12-17 03:44:29 +00:00
|
|
|
const setupProvider = async function (clientId) {
|
2022-11-09 23:15:31 +00:00
|
|
|
const providerName = `my-provider`;
|
2021-11-10 21:19:40 +00:00
|
|
|
await consoleComponent.runCommands(
|
|
|
|
`write identity/oidc/provider/${providerName} allowed_client_ids="${clientId}" scopes="user,groups"`
|
|
|
|
);
|
|
|
|
return providerName;
|
|
|
|
};
|
|
|
|
|
|
|
|
const getAuthzUrl = (providerName, redirect, clientId, params) => {
|
2022-11-09 23:15:31 +00:00
|
|
|
const queryParams = {
|
2021-11-10 21:19:40 +00:00
|
|
|
client_id: clientId,
|
|
|
|
nonce: 'abc123',
|
|
|
|
redirect_uri: encodeURIComponent(redirect),
|
|
|
|
response_type: 'code',
|
|
|
|
scope: 'openid',
|
|
|
|
state: 'foobar',
|
|
|
|
...params,
|
|
|
|
};
|
2022-11-09 23:15:31 +00:00
|
|
|
const queryString = Object.keys(queryParams).reduce((prev, key, idx) => {
|
2021-11-10 21:19:40 +00:00
|
|
|
if (idx === 0) {
|
|
|
|
return `${prev}${key}=${queryParams[key]}`;
|
|
|
|
}
|
|
|
|
return `${prev}&${key}=${queryParams[key]}`;
|
|
|
|
}, '?');
|
|
|
|
return `/vault/identity/oidc/provider/${providerName}/authorize${queryString}`;
|
|
|
|
};
|
|
|
|
|
2023-03-22 18:19:11 +00:00
|
|
|
const setupOidc = async function (uid) {
|
2021-11-10 21:19:40 +00:00
|
|
|
const callback = 'http://127.0.0.1:8251/callback';
|
|
|
|
const entityId = await oidcEntity('oidc', OIDC_POLICY);
|
|
|
|
const groupId = await oidcGroup(entityId);
|
2023-03-22 18:19:11 +00:00
|
|
|
const authMethodPath = `oidc-userpass-${uid}`;
|
2021-11-10 21:19:40 +00:00
|
|
|
const accessor = await authAccessor(authMethodPath);
|
|
|
|
await entityAlias(entityId, accessor, groupId);
|
|
|
|
const clientId = await setupWebapp(callback);
|
2022-11-09 23:15:31 +00:00
|
|
|
const providerName = await setupProvider(clientId);
|
2021-11-10 21:19:40 +00:00
|
|
|
return {
|
|
|
|
providerName,
|
|
|
|
callback,
|
|
|
|
clientId,
|
|
|
|
authMethodPath,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
module('Acceptance | oidc provider', function (hooks) {
|
2021-11-10 21:19:40 +00:00
|
|
|
setupApplicationTest(hooks);
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
hooks.beforeEach(async function () {
|
2023-03-22 18:19:11 +00:00
|
|
|
this.uid = uuidv4();
|
2022-09-09 01:06:05 +00:00
|
|
|
this.store = await this.owner.lookup('service:store');
|
2021-11-10 21:19:40 +00:00
|
|
|
await logout.visit();
|
|
|
|
return authPage.login();
|
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('OIDC Provider logs in and redirects correctly', async function (assert) {
|
2023-03-22 18:19:11 +00:00
|
|
|
const { providerName, callback, clientId, authMethodPath } = await setupOidc(this.uid);
|
2021-11-10 21:19:40 +00:00
|
|
|
|
|
|
|
await logout.visit();
|
|
|
|
await settled();
|
2022-11-09 23:15:31 +00:00
|
|
|
const url = getAuthzUrl(providerName, callback, clientId);
|
2021-12-17 03:44:29 +00:00
|
|
|
await visit(url);
|
|
|
|
|
2021-11-10 21:19:40 +00:00
|
|
|
assert.ok(currentURL().startsWith('/vault/auth'), 'redirects to auth when no token');
|
|
|
|
assert.ok(
|
|
|
|
currentURL().includes(`redirect_to=${encodeURIComponent(url)}`),
|
|
|
|
'encodes url for the query param'
|
|
|
|
);
|
|
|
|
assert.dom('[data-test-auth-logo]').exists('Vault logo exists on auth page');
|
|
|
|
assert
|
|
|
|
.dom('[data-test-auth-helptext]')
|
|
|
|
.hasText(
|
|
|
|
'Once you log in, you will be redirected back to your application. If you require login credentials, contact your administrator.',
|
|
|
|
'Has updated text for client authorization flow'
|
|
|
|
);
|
|
|
|
await authFormComponent.selectMethod(authMethodPath);
|
|
|
|
await authFormComponent.username(OIDC_USER);
|
|
|
|
await authFormComponent.password(USER_PASSWORD);
|
|
|
|
await authFormComponent.login();
|
|
|
|
await settled();
|
2022-10-18 15:46:02 +00:00
|
|
|
assert.strictEqual(currentURL(), url, 'URL is as expected after login');
|
2021-11-10 21:19:40 +00:00
|
|
|
assert.dom('[data-test-oidc-redirect]').exists('redirect text exists');
|
2022-02-24 20:04:40 +00:00
|
|
|
assert
|
|
|
|
.dom('[data-test-oidc-redirect]')
|
|
|
|
.hasTextContaining(`${callback}?code=`, 'Successful redirect to callback');
|
2022-09-09 01:06:05 +00:00
|
|
|
|
|
|
|
//* clean up test state
|
|
|
|
await clearRecord(this.store, 'oidc/client', 'my-webapp');
|
|
|
|
await clearRecord(this.store, 'oidc/provider', 'my-provider');
|
2021-11-10 21:19:40 +00:00
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('OIDC Provider redirects to auth if current token and prompt = login', async function (assert) {
|
2023-03-22 18:19:11 +00:00
|
|
|
const { providerName, callback, clientId, authMethodPath } = await setupOidc(this.uid);
|
2021-11-10 21:19:40 +00:00
|
|
|
await settled();
|
|
|
|
const url = getAuthzUrl(providerName, callback, clientId, { prompt: 'login' });
|
2021-12-17 03:44:29 +00:00
|
|
|
await visit(url);
|
|
|
|
|
2021-11-10 21:19:40 +00:00
|
|
|
assert.ok(currentURL().startsWith('/vault/auth'), 'redirects to auth when no token');
|
|
|
|
assert.notOk(
|
|
|
|
currentURL().includes('prompt=login'),
|
|
|
|
'Url params no longer include prompt=login after redirect'
|
|
|
|
);
|
|
|
|
await authFormComponent.selectMethod(authMethodPath);
|
|
|
|
await authFormComponent.username(OIDC_USER);
|
|
|
|
await authFormComponent.password(USER_PASSWORD);
|
|
|
|
await authFormComponent.login();
|
|
|
|
await settled();
|
2022-02-24 20:04:40 +00:00
|
|
|
assert
|
|
|
|
.dom('[data-test-oidc-redirect]')
|
|
|
|
.hasTextContaining(`${callback}?code=`, 'Successful redirect to callback');
|
2022-09-09 01:06:05 +00:00
|
|
|
|
|
|
|
//* clean up test state
|
|
|
|
await clearRecord(this.store, 'oidc/client', 'my-webapp');
|
|
|
|
await clearRecord(this.store, 'oidc/provider', 'my-provider');
|
2021-11-10 21:19:40 +00:00
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('OIDC Provider shows consent form when prompt = consent', async function (assert) {
|
2023-03-22 18:19:11 +00:00
|
|
|
const { providerName, callback, clientId, authMethodPath } = await setupOidc(this.uid);
|
2021-11-10 21:19:40 +00:00
|
|
|
const url = getAuthzUrl(providerName, callback, clientId, { prompt: 'consent' });
|
|
|
|
await logout.visit();
|
|
|
|
await authFormComponent.selectMethod(authMethodPath);
|
|
|
|
await authFormComponent.username(OIDC_USER);
|
|
|
|
await authFormComponent.password(USER_PASSWORD);
|
|
|
|
await authFormComponent.login();
|
2021-12-17 03:44:29 +00:00
|
|
|
await visit(url);
|
|
|
|
|
2021-11-10 21:19:40 +00:00
|
|
|
assert.notOk(
|
|
|
|
currentURL().startsWith('/vault/auth'),
|
|
|
|
'Does not redirect to auth because user is already logged in'
|
|
|
|
);
|
|
|
|
assert.dom('[data-test-consent-form]').exists('Consent form exists');
|
2022-09-09 01:06:05 +00:00
|
|
|
|
|
|
|
//* clean up test state
|
|
|
|
await clearRecord(this.store, 'oidc/client', 'my-webapp');
|
|
|
|
await clearRecord(this.store, 'oidc/provider', 'my-provider');
|
2021-11-10 21:19:40 +00:00
|
|
|
});
|
|
|
|
});
|