Skip to content

Instantly share code, notes, and snippets.

@joelhooks
Forked from panva/README.md
Last active October 5, 2023 03:04
Show Gist options
  • Select an option

  • Save joelhooks/c585cdab12b986fc7f1a057796e99b76 to your computer and use it in GitHub Desktop.

Select an option

Save joelhooks/c585cdab12b986fc7f1a057796e99b76 to your computer and use it in GitHub Desktop.

Revisions

  1. joelhooks revised this gist Oct 5, 2023. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@
    "author": "",
    "license": "ISC",
    "dependencies": {
    "open": "^7.0.0",
    "openid-client": "^3.8.3"
    "open": "^9.1.0",
    "openid-client": "^5.6.0"
    }
    }
  2. joelhooks revised this gist Oct 5, 2023. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -7,11 +7,10 @@
    "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
    },
    "keywords": [],
    "author": "",
    "license": "ISC",
    "dependencies": {
    "open": "^9.1.0",
    "openid-client": "^5.6.0"
    "open": "^7.0.0",
    "openid-client": "^3.8.3"
    }
    }
    }
  3. joelhooks revised this gist Oct 5, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion package.json
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    {
    "name": "test-auth",
    "name": "pg",
    "version": "1.0.0",
    "description": "",
    "main": "index.mjs",
  4. joelhooks revised this gist Oct 5, 2023. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,7 @@
    "version": "1.0.0",
    "description": "",
    "main": "index.mjs",
    "bin": "./index.mjs",
    "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
    },
  5. joelhooks revised this gist Oct 5, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -3,5 +3,5 @@
    run

    ```sh
    npx https://gist.github.com/panva/ebaacfe433a8677bdbf458f6e1132045
    npx https://gist.github.com/joelhooks/c585cdab12b986fc7f1a057796e99b76
    ```
  6. joelhooks revised this gist Oct 5, 2023. 2 changed files with 26 additions and 26 deletions.
    42 changes: 21 additions & 21 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -2,35 +2,35 @@

    /* eslint-disable no-console, camelcase */

    const { Issuer } = require('openid-client');
    const open = require('open');
    import { Issuer } from 'openid-client';
    import open from 'open';

    const { ISSUER = 'https://op.panva.cz' } = process.env;
    const { ISSUER = 'https://www.epicweb.dev/oauth' } = process.env;
    const GRANT_TYPE = 'urn:ietf:params:oauth:grant-type:device_code';

    (async () => {
    const issuer = await Issuer.discover(ISSUER);
    const issuer = await Issuer.discover(ISSUER);

    const client = await issuer.Client.register({
    grant_types: [GRANT_TYPE],
    response_types: [],
    redirect_uris: [],
    token_endpoint_auth_method: 'none',
    application_type: 'native',
    });
    const client = await issuer.Client.register({
    grant_types: [GRANT_TYPE],
    response_types: [],
    redirect_uris: [],
    token_endpoint_auth_method: 'none',
    application_type: 'native',
    });

    const handle = await client.deviceAuthorization();
    const handle = await client.deviceAuthorization();

    await open(handle.verification_uri_complete, { wait: false });
    await open(handle.verification_uri_complete, { wait: false });

    const tokenSet = await handle.poll();
    const tokenSet = await handle.poll();

    console.log('got', tokenSet);
    console.log('id token claims', tokenSet.claims());
    console.log('got', tokenSet);
    // console.log('id token claims', tokenSet.claims());

    const userinfo = await client.userinfo(tokenSet);
    console.log('userinfo', userinfo);
    // const userinfo = await client.userinfo(tokenSet);
    // console.log('userinfo', userinfo);
    })().catch((err) => {
    console.error(err);
    process.exitCode = 1;
    });
    console.error(err);
    process.exitCode = 1;
    });
    10 changes: 5 additions & 5 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,16 @@
    {
    "name": "pg",
    "name": "test-auth",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "bin": "./index.js",
    "main": "index.mjs",
    "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
    },
    "keywords": [],
    "author": "",
    "license": "ISC",
    "dependencies": {
    "open": "^7.0.0",
    "openid-client": "^3.8.3"
    "open": "^9.1.0",
    "openid-client": "^5.6.0"
    }
    }
  7. @panva panva revised this gist Apr 29, 2021. 3 changed files with 11 additions and 1 deletion.
    7 changes: 7 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    # Simple Device Flow Login CLI implementation

    run

    ```sh
    npx https://gist.github.com/panva/ebaacfe433a8677bdbf458f6e1132045
    ```
    2 changes: 2 additions & 0 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    #!/usr/bin/env node

    /* eslint-disable no-console, camelcase */

    const { Issuer } = require('openid-client');
    3 changes: 2 additions & 1 deletion package.json
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,8 @@
    "name": "pg",
    "version": "1.0.0",
    "description": "",
    "main": "codeflow.js",
    "main": "index.js",
    "bin": "./index.js",
    "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
    },
  8. @panva panva revised this gist Nov 22, 2019. 2 changed files with 6 additions and 6 deletions.
    4 changes: 2 additions & 2 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    /* eslint-disable no-console, camelcase */

    const { Issuer } = require('openid-client');
    const opn = require('opn');
    const open = require('open');

    const { ISSUER = 'https://op.panva.cz' } = process.env;
    const GRANT_TYPE = 'urn:ietf:params:oauth:grant-type:device_code';
    @@ -19,7 +19,7 @@ const GRANT_TYPE = 'urn:ietf:params:oauth:grant-type:device_code';

    const handle = await client.deviceAuthorization();

    await opn(handle.verification_uri_complete, { wait: false });
    await open(handle.verification_uri_complete, { wait: false });

    const tokenSet = await handle.poll();

    8 changes: 4 additions & 4 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,15 @@
    {
    "name": "example",
    "name": "pg",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "main": "codeflow.js",
    "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
    },
    "author": "",
    "license": "ISC",
    "dependencies": {
    "openid-client": "^3.8.3",
    "opn": "^5.3.0"
    "open": "^7.0.0",
    "openid-client": "^3.8.3"
    }
    }
  9. @panva panva revised this gist Nov 22, 2019. 2 changed files with 6 additions and 44 deletions.
    46 changes: 5 additions & 41 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -1,19 +1,13 @@
    /* eslint-disable no-console, camelcase */

    const { randomBytes, createHash } = require('crypto');

    const { encode } = require('base64url');
    const { post } = require('got');
    const { Issuer } = require('openid-client');
    const opn = require('opn');

    const { ISSUER = 'https://guarded-cliffs-8635.herokuapp.com' } = process.env;
    const { ISSUER = 'https://op.panva.cz' } = process.env;
    const GRANT_TYPE = 'urn:ietf:params:oauth:grant-type:device_code';
    const SOFT_ERRORS = new Set(['authorization_pending', 'slow_down']);

    (async () => {
    const issuer = await Issuer.discover(ISSUER);
    const { device_authorization_endpoint } = issuer;

    const client = await issuer.Client.register({
    grant_types: [GRANT_TYPE],
    @@ -23,44 +17,14 @@ const SOFT_ERRORS = new Set(['authorization_pending', 'slow_down']);
    application_type: 'native',
    });

    const code_verifier = randomBytes(8).toString('hex');
    const code_challenge = encode(createHash('sha256').update(code_verifier).digest());

    const {
    body: { device_code, verification_uri_complete },
    } = await post(device_authorization_endpoint, {
    json: true,
    form: true,
    body: {
    client_id: client.client_id,
    scope: 'openid email',
    code_challenge,
    code_challenge_method: 'S256',
    claims: JSON.stringify({ id_token: { email: null } }),
    },
    });
    const handle = await client.deviceAuthorization();

    await opn(verification_uri_complete, { wait: false });
    await opn(handle.verification_uri_complete, { wait: false });

    const tokenSet = await new Promise((resolve, reject) => {
    function poll() {
    client.grant({
    grant_type: GRANT_TYPE,
    device_code,
    code_verifier,
    }).then(resolve, (err) => {
    if (err.name === 'OpenIdConnectError' && SOFT_ERRORS.has(err.error)) {
    setTimeout(poll, 3000);
    } else {
    reject(err);
    }
    });
    }
    setTimeout(poll, 3000);
    });
    const tokenSet = await handle.poll();

    console.log('got', tokenSet);
    console.log('id token claims', tokenSet.claims);
    console.log('id token claims', tokenSet.claims());

    const userinfo = await client.userinfo(tokenSet);
    console.log('userinfo', userinfo);
    4 changes: 1 addition & 3 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -9,9 +9,7 @@
    "author": "",
    "license": "ISC",
    "dependencies": {
    "base64url": "^3.0.0",
    "got": "^8.3.2",
    "openid-client": "^2.2.1",
    "openid-client": "^3.8.3",
    "opn": "^5.3.0"
    }
    }
  10. @panva panva revised this gist Jul 23, 2018. 1 changed file with 17 additions and 0 deletions.
    17 changes: 17 additions & 0 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    {
    "name": "example",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
    },
    "author": "",
    "license": "ISC",
    "dependencies": {
    "base64url": "^3.0.0",
    "got": "^8.3.2",
    "openid-client": "^2.2.1",
    "opn": "^5.3.0"
    }
    }
  11. @panva panva renamed this gist Jul 23, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  12. @panva panva created this gist Jul 23, 2018.
    70 changes: 70 additions & 0 deletions device_flow.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    /* eslint-disable no-console, camelcase */

    const { randomBytes, createHash } = require('crypto');

    const { encode } = require('base64url');
    const { post } = require('got');
    const { Issuer } = require('openid-client');
    const opn = require('opn');

    const { ISSUER = 'https://guarded-cliffs-8635.herokuapp.com' } = process.env;
    const GRANT_TYPE = 'urn:ietf:params:oauth:grant-type:device_code';
    const SOFT_ERRORS = new Set(['authorization_pending', 'slow_down']);

    (async () => {
    const issuer = await Issuer.discover(ISSUER);
    const { device_authorization_endpoint } = issuer;

    const client = await issuer.Client.register({
    grant_types: [GRANT_TYPE],
    response_types: [],
    redirect_uris: [],
    token_endpoint_auth_method: 'none',
    application_type: 'native',
    });

    const code_verifier = randomBytes(8).toString('hex');
    const code_challenge = encode(createHash('sha256').update(code_verifier).digest());

    const {
    body: { device_code, verification_uri_complete },
    } = await post(device_authorization_endpoint, {
    json: true,
    form: true,
    body: {
    client_id: client.client_id,
    scope: 'openid email',
    code_challenge,
    code_challenge_method: 'S256',
    claims: JSON.stringify({ id_token: { email: null } }),
    },
    });

    await opn(verification_uri_complete, { wait: false });

    const tokenSet = await new Promise((resolve, reject) => {
    function poll() {
    client.grant({
    grant_type: GRANT_TYPE,
    device_code,
    code_verifier,
    }).then(resolve, (err) => {
    if (err.name === 'OpenIdConnectError' && SOFT_ERRORS.has(err.error)) {
    setTimeout(poll, 3000);
    } else {
    reject(err);
    }
    });
    }
    setTimeout(poll, 3000);
    });

    console.log('got', tokenSet);
    console.log('id token claims', tokenSet.claims);

    const userinfo = await client.userinfo(tokenSet);
    console.log('userinfo', userinfo);
    })().catch((err) => {
    console.error(err);
    process.exitCode = 1;
    });