|
| 1 | +// Copyright © 2022 Ory Corp |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import { prng } from "../../helpers" |
| 5 | + |
| 6 | +const accessTokenStrategies = ["opaque", "jwt"] |
| 7 | + |
| 8 | +describe("The OAuth 2.0 Device Authorization Grant", function () { |
| 9 | + accessTokenStrategies.forEach((accessTokenStrategy) => { |
| 10 | + describe("access_token_strategy=" + accessTokenStrategy, function () { |
| 11 | + const nc = (extradata) => ({ |
| 12 | + client_secret: prng(), |
| 13 | + scope: "offline_access openid", |
| 14 | + subject_type: "public", |
| 15 | + token_endpoint_auth_method: "client_secret_basic", |
| 16 | + grant_types: [ |
| 17 | + "urn:ietf:params:oauth:grant-type:device_code", |
| 18 | + "refresh_token", |
| 19 | + ], |
| 20 | + access_token_strategy: accessTokenStrategy, |
| 21 | + ...extradata, |
| 22 | + }) |
| 23 | + |
| 24 | + it("should return an Access, Refresh, and ID Token when scope offline_access and openid are granted", function () { |
| 25 | + const client = nc() |
| 26 | + cy.deviceAuthFlow(client, { |
| 27 | + consent: { scope: ["offline_access", "openid"] }, |
| 28 | + }) |
| 29 | + |
| 30 | + cy.postDeviceAuthFlow().then((resp) => { |
| 31 | + const { |
| 32 | + result, |
| 33 | + token: { access_token, id_token, refresh_token }, |
| 34 | + } = resp.body |
| 35 | + |
| 36 | + expect(result).to.equal("success") |
| 37 | + expect(access_token).to.not.be.empty |
| 38 | + expect(id_token).to.not.be.empty |
| 39 | + expect(refresh_token).to.not.be.empty |
| 40 | + }) |
| 41 | + }) |
| 42 | + |
| 43 | + it("should return an Access and Refresh Token when scope offline_access is granted", function () { |
| 44 | + const client = nc() |
| 45 | + cy.deviceAuthFlow(client, { consent: { scope: ["offline_access"] } }) |
| 46 | + |
| 47 | + cy.postDeviceAuthFlow().then((resp) => { |
| 48 | + console.log(resp) |
| 49 | + const { |
| 50 | + result, |
| 51 | + token: { access_token, id_token, refresh_token }, |
| 52 | + } = resp.body |
| 53 | + |
| 54 | + expect(result).to.equal("success") |
| 55 | + expect(access_token).to.not.be.empty |
| 56 | + expect(id_token).to.be.undefined |
| 57 | + expect(refresh_token).to.not.be.empty |
| 58 | + }) |
| 59 | + }) |
| 60 | + |
| 61 | + it("should return an Access and ID Token when scope offline_access is granted", function () { |
| 62 | + const client = nc() |
| 63 | + cy.deviceAuthFlow(client, { consent: { scope: ["openid"] } }) |
| 64 | + |
| 65 | + cy.postDeviceAuthFlow().then((resp) => { |
| 66 | + console.log(resp) |
| 67 | + const { |
| 68 | + result, |
| 69 | + token: { access_token, id_token, refresh_token }, |
| 70 | + } = resp.body |
| 71 | + |
| 72 | + expect(result).to.equal("success") |
| 73 | + expect(access_token).to.not.be.empty |
| 74 | + expect(id_token).to.not.be.empty |
| 75 | + expect(refresh_token).to.be.undefined |
| 76 | + }) |
| 77 | + }) |
| 78 | + |
| 79 | + it("should return an Access Token when no scope is granted", function () { |
| 80 | + const client = nc() |
| 81 | + cy.deviceAuthFlow(client, { consent: { scope: [] } }) |
| 82 | + |
| 83 | + cy.postDeviceAuthFlow().then((resp) => { |
| 84 | + console.log(resp) |
| 85 | + const { |
| 86 | + result, |
| 87 | + token: { access_token, id_token, refresh_token }, |
| 88 | + } = resp.body |
| 89 | + |
| 90 | + expect(result).to.equal("success") |
| 91 | + expect(access_token).to.not.be.empty |
| 92 | + expect(id_token).to.be.undefined |
| 93 | + expect(refresh_token).to.be.undefined |
| 94 | + }) |
| 95 | + }) |
| 96 | + |
| 97 | + it("should skip consent if the client is confgured thus", function () { |
| 98 | + const client = nc({ skip_consent: true }) |
| 99 | + cy.deviceAuthFlow(client, { |
| 100 | + consent: { scope: ["offline_access", "openid"], skip: true }, |
| 101 | + }) |
| 102 | + |
| 103 | + cy.postDeviceAuthFlow().then((resp) => { |
| 104 | + console.log(resp) |
| 105 | + const { |
| 106 | + result, |
| 107 | + token: { access_token, id_token, refresh_token }, |
| 108 | + } = resp.body |
| 109 | + |
| 110 | + expect(result).to.equal("success") |
| 111 | + expect(access_token).to.not.be.empty |
| 112 | + expect(id_token).to.not.be.empty |
| 113 | + expect(refresh_token).to.not.be.empty |
| 114 | + }) |
| 115 | + }) |
| 116 | + }) |
| 117 | + }) |
| 118 | +}) |
0 commit comments