Last active
October 20, 2024 09:23
Simple Device Flow Login CLI implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable no-console, camelcase */ | |
const { Issuer } = require('openid-client'); | |
const opn = require('opn'); | |
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 opn(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; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "example", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"openid-client": "^3.8.3", | |
"opn": "^5.3.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment