Skip to content

Instantly share code, notes, and snippets.

@panva
Last active October 5, 2023 02:47
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save panva/ebaacfe433a8677bdbf458f6e1132045 to your computer and use it in GitHub Desktop.
Save panva/ebaacfe433a8677bdbf458f6e1132045 to your computer and use it in GitHub Desktop.
Simple Device Flow Login CLI implementation

Simple Device Flow Login CLI implementation

run

npx https://gist.github.com/panva/ebaacfe433a8677bdbf458f6e1132045
#!/usr/bin/env node
/* eslint-disable no-console, camelcase */
const { Issuer } = require('openid-client');
const open = require('open');
const { ISSUER = 'https://op.panva.cz' } = process.env;
const GRANT_TYPE = 'urn:ietf:params:oauth:grant-type:device_code';
(async () => {
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 handle = await client.deviceAuthorization();
await open(handle.verification_uri_complete, { wait: false });
const tokenSet = await handle.poll();
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;
});
{
"name": "pg",
"version": "1.0.0",
"description": "",
"main": "index.js",
"bin": "./index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"open": "^7.0.0",
"openid-client": "^3.8.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment