Skip to content

Commit

Permalink
login
Browse files Browse the repository at this point in the history
  • Loading branch information
babangsund committed Nov 13, 2019
1 parent 8936c34 commit eb1397b
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 3 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"cypress": "^3.6.1",
"eslint": "^6.6.0",
"eslint-config-prettier": "^6.5.0",
"prettier": "^1.19.1"
"prettier": "^1.19.1",
"typescript": "^3.7.2"
}
}
35 changes: 33 additions & 2 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,42 @@ declare namespace Cypress {
realm: string;
redirect_uri: string;
}
interface Login {
root: string;
realm: string;
username: string;
password: string;
client_id: string;
redirect_uri: string;
}
interface Chainable {
/**
* Command to sign out of keycloak
* @example cy.logout({ realm: "..", redirect_uri: "...", root: "..." })
* Command to sign out of Keycloak
* @example cy.logout({
* root: "...",
* realm: "..",
* redirect_uri: "..."
* });
*/
logout({ root, realm, redirect_uri }: Logout): Chainable;
/**
* Command to sign into Keycloak
* @example cy.logout({
* root: "...",
* realm: "..",
* username: "...",
* password: "...",
* client_id: "...",
* redirect_uri: "..."
* });
*/
login({
root,
realm,
username,
password,
client_id,
redirect_uri,
}: Login): Chainable;
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
import './login';
import './logout';
53 changes: 53 additions & 0 deletions src/login.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
function createUUID(): string {
const s: string[] = [];
const hexDigits = '0123456789abcdef';

for (let i = 0; i < 36; i++) {
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
}

s[14] = '4';
s[19] = hexDigits.substr((parseInt(s[19]) & 0x3) | 0x8, 1);
s[8] = s[13] = s[18] = s[23] = '-';

return s.join('');
}

Cypress.Commands.add(
'login',
({ root, realm, username, password, client_id, redirect_uri }) => {
return cy
.request({
url: `${root}/auth/realms/${realm}/protocol/openid-connect/auth`,
qs: {
client_id,
redirect_uri,
scope: 'openid',
state: createUUID(),
nonce: createUUID(),
response_type: 'code',
response_mode: 'fragment',
},
})
.then(response => {
const html = document.createElement('html');
html.innerHTML = response.body;

const form = html.getElementsByTagName('form');
const isAuthorized = !form.length;

if (!isAuthorized) {
return cy.request({
form: true,
method: 'POST',
url: form[0].action,
followRedirect: false,
body: {
username: username,
password: password,
},
});
}
});
}
);
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1791,6 +1791,11 @@ typedarray@^0.0.6:
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=

typescript@^3.7.2:
version "3.7.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.7.2.tgz#27e489b95fa5909445e9fef5ee48d81697ad18fb"
integrity sha512-ml7V7JfiN2Xwvcer+XAf2csGO1bPBdRbFCkYBczNZggrBZ9c7G3riSUeJmqEU5uOtXNPMhE3n+R4FA/3YOAWOQ==

universalify@^0.1.0:
version "0.1.2"
resolved "https://registry.yarnpkg.com/universalify/-/universalify-0.1.2.tgz#b646f69be3942dabcecc9d6639c80dc105efaa66"
Expand Down

0 comments on commit eb1397b

Please sign in to comment.