Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OAuth2 client server authentication for a command line utility

I'm working on an command line utility that requires access to a backend server over a REST API.

I'm trying to avoid implementing my own authentication mechanism and use one (or more) of the public auth services (like google, Facebook, amazon).

I am trying to make the client accept credentials and authenticate against the authentication provider and do that without asking the user go open a web browser and provide back a token. The client will be open sourced in order to avoid trust issues (i.e. the user credentials are not sent to my backend server).

I am not interested in authorization, I only care of authenticating against my backend server without having the user keep yet another set of credentials (and without sending the user credentials to my backend server).

How can I have my client authenticate with the auth provider and get a token to communicate back with my server without having the user use a web browser?

like image 646
Pavel Georgiev Avatar asked Mar 17 '26 16:03

Pavel Georgiev


1 Answers

I realize you said "not open a web browser", but what about if that browser is on another device (e.g. their mobile?).

If that is acceptable, you can use the OAuth 2.0 for Devices approach, whereby you present the user a short alphanumeric code, which they enter on http://google.com/device to authenticate the request from another device. This OAuth flow is designed to work in environments which don't have browsers (like a command line).

To see a demo of this authentication flow in action, visit the YouTube TV site, press the ← key on your keyboard, and select Sign In.

It's also easy to try out yourself – create a OAuth client in the developers console (of type "installed application" -> "other"), and follow the curl examples in the docs (be sure to replace the demo code in the token request with the device_code received from the initial request to the code endpoint). Decode the resulting id_token using any of the example JWT decoders, like this one.

In your case, you should request the profile scope, which will return an id_token in the response to your token endpoint call, from which you can extract the user's Google profile id (the id token's sub field).

like image 61
William Denniss Avatar answered Mar 20 '26 05:03

William Denniss



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!