From f3d09f8bda294a82cbeb989e89ae7384ea6543c9 Mon Sep 17 00:00:00 2001 From: Patrick van Zadel Date: Mon, 8 Jun 2020 15:57:32 +0200 Subject: [PATCH 1/4] Add client_secret to kcLogin command and tested agains Keycloak 9.0.1 and Keycloak 10.0.2 --- README.md | 11 ++++++++--- src/kc-login.ts | 4 +++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 7729215..d618efc 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,9 @@ # cypress-keycloak-commands + + [![All Contributors](https://img.shields.io/badge/all_contributors-2-orange.svg?style=flat-square)](#contributors-) + Cypress commands for login with [Keycloak](https://www.keycloak.org/). @@ -9,7 +12,7 @@ Cypress commands for login with [Keycloak](https://www.keycloak.org/). - Use Fixtures to store users data - Returns you the tokens of the logged user for calling backend APIs from your test code - Fake login command for integration testing -- Tested with Keycloak 4.8, 5, 6, 7 and 8 +- Tested with Keycloak 4.8, 5, 6, 7, 8, 9 and 10 ## Usage @@ -42,7 +45,8 @@ Setup the Keycloak configuration in `cypress.json` configuration file: "env": { "auth_base_url": "https://auth.server/auth", "auth_realm": "my_realm", - "auth_client_id": "my_client_id" + "auth_client_id": "my_client_id", + "auth_client_secret": "my_client_secret" } } ``` @@ -183,6 +187,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d + -This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome! \ No newline at end of file +This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome! diff --git a/src/kc-login.ts b/src/kc-login.ts index 0ba4aa9..faf8724 100644 --- a/src/kc-login.ts +++ b/src/kc-login.ts @@ -7,6 +7,7 @@ Cypress.Commands.add("kcLogin", (user: string) => { const authBaseUrl = Cypress.env("auth_base_url"); const realm = Cypress.env("auth_realm"); const client_id = Cypress.env("auth_client_id"); + const client_secret = Cypress.env("auth_client_secret"); cy.request({ url: `${authBaseUrl}/realms/${realm}/protocol/openid-connect/auth`, @@ -16,7 +17,8 @@ Cypress.Commands.add("kcLogin", (user: string) => { response_type: "code", approval_prompt: "auto", redirect_uri: Cypress.config("baseUrl"), - client_id + client_id, + client_secret } }) .then(response => { From 2cfe282a103cdd1db9689c3d286463df0a33d04e Mon Sep 17 00:00:00 2001 From: Patrick van Zadel Date: Mon, 8 Jun 2020 15:59:06 +0200 Subject: [PATCH 2/4] Updated keycloak version in docker-compose.yml --- testing/docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/docker-compose.yml b/testing/docker-compose.yml index 6762055..96a8d8e 100644 --- a/testing/docker-compose.yml +++ b/testing/docker-compose.yml @@ -2,7 +2,7 @@ version: "3.7" services: keycloak: - image: jboss/keycloak:${KEYCLOAK_VERSION:-8.0.1} + image: jboss/keycloak:${KEYCLOAK_VERSION:-10.0.2} environment: - KEYCLOAK_IMPORT=/tmp/example-realm.json volumes: From 7274a02789181c68155a5491d07fa2e04ac85435 Mon Sep 17 00:00:00 2001 From: Patrick van Zadel Date: Mon, 8 Jun 2020 16:57:26 +0200 Subject: [PATCH 3/4] Added the client_secret to the last then method --- src/kc-login.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/kc-login.ts b/src/kc-login.ts index faf8724..ab7c004 100644 --- a/src/kc-login.ts +++ b/src/kc-login.ts @@ -47,6 +47,7 @@ Cypress.Commands.add("kcLogin", (user: string) => { url: `${authBaseUrl}/realms/${realm}/protocol/openid-connect/token`, body: { client_id, + client_secret, redirect_uri: Cypress.config("baseUrl"), code, grant_type: "authorization_code" From 43998de41c431886f1af4fd148dc48a892102015 Mon Sep 17 00:00:00 2001 From: Patrick van Zadel Date: Tue, 9 Jun 2020 12:00:31 +0200 Subject: [PATCH 4/4] Refactored the code to make `client_secret` optional --- src/kc-login.ts | 13 +++---------- src/utils.ts | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/kc-login.ts b/src/kc-login.ts index ab7c004..f0465e0 100644 --- a/src/kc-login.ts +++ b/src/kc-login.ts @@ -1,4 +1,4 @@ -import { getAuthCodeFromLocation } from "./utils"; +import {createRequestBodyForToken, getAuthCodeFromLocation} from './utils'; Cypress.Commands.add("kcLogin", (user: string) => { Cypress.log({ name: "Login" }); @@ -17,8 +17,7 @@ Cypress.Commands.add("kcLogin", (user: string) => { response_type: "code", approval_prompt: "auto", redirect_uri: Cypress.config("baseUrl"), - client_id, - client_secret + client_id } }) .then(response => { @@ -45,13 +44,7 @@ Cypress.Commands.add("kcLogin", (user: string) => { cy.request({ method: "post", url: `${authBaseUrl}/realms/${realm}/protocol/openid-connect/token`, - body: { - client_id, - client_secret, - redirect_uri: Cypress.config("baseUrl"), - code, - grant_type: "authorization_code" - }, + body: createRequestBodyForToken(client_id, client_secret, Cypress.config("baseUrl"), code), form: true, followRedirect: false }).its("body"); diff --git a/src/utils.ts b/src/utils.ts index 5440dc0..4b1789a 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -23,6 +23,25 @@ export function getAuthCodeFromLocation(location: string): string | undefined { } } +export function createRequestBodyForToken(client_id: string, client_secret: string | undefined, baseUrl: string | null, code: string | undefined): object { + if (client_secret) { + return { + client_id, + client_secret, + redirect_uri: baseUrl, + code, + grant_type: "authorization_code" + } + } + + return { + client_id, + redirect_uri: baseUrl, + code, + grant_type: "authorization_code" + } +} + export function decodeToken(str: string): { nonce: string } { str = str.split(".")[1];