Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): prompt user for authentication on subsequent logins (#11004, #9329) #12883

Closed
wants to merge 3 commits into from

Conversation

elasticspoon
Copy link
Contributor

@elasticspoon elasticspoon commented Apr 6, 2024

Fixes #11004 (partially)

coder login When Already Authenticated

Currently if a user runs coder login when logged in they will get the default login flow:

$ > coder login http://localhost:8080
Attempting to authenticate with argument URL: 'http://localhost:8080'
Your browser has been opened to visit:

        http://localhost:8080/cli-auth

> Paste your token here: DqSDeIXhgx-ABpLuIJ5PnLJjtvdUwmRK5
> Welcome to Coder, admin! You're authenticated.

Instead prompt the user if they want to re-authenticate if they run coder login when already authenticated. Authenticated is defined as having a valid session token stored in the config directory.

$ > coder login http://localhost:8080
Attempting to authenticate with argument URL: 'http://localhost:8080'
> You are already authenticated admin. Are you sure you want to log in again? (yes/no) no

Does not apply if they provided a valid token via flag or ENV variable. A new token should be generated an stored in that situation unless the --use-token-as-session flag was also used.

Invalid Token

If an invalid token was set as ENV variable a user would be unable to login until the ENV variable was cleared. (The same behavior will occur for invalid flags)

User will now be informed that the token is invalid and prompted to login normally. The ENV variable won't be cleared but a valid session token will be created.

I was incorrect about this part. While we can create a new valid token at login subsequent commands will still prefer the invalid ENV token. A potential solution would be to check if the ENV token if valid and fall back to the config token in that situation, however, that creates the overhead of an additional API call on every authed CLI command invocation.

@cdr-bot cdr-bot bot added the community Pull Requests created by the community. label Apr 6, 2024
@elasticspoon elasticspoon changed the title feat(cli): prompt on subsequent logins as authenticated user feat(cli): prompt authenticated user on subsequent logins Apr 6, 2024
)

Prompt the user if they want to re-authenticate if they run
`coder login` when already authenticated.

Does not apply if they provided a token via flag or ENV
variable. A new token should be generated an stored in that situation
unless the `--use-token-as-session` flag was also used.

fix(cli): prompt user on login attempt with invalid ENV token

Fixes issue where if invalid token was set as ENV
variable a user would be unable to login until the ENV
variable was cleared.

User will now be informed that the token is invalid and
prompted to login normally.
@elasticspoon elasticspoon changed the title feat(cli): prompt authenticated user on subsequent logins feat(cli): prompt authenticated user on subsequent logins (#11004, #9329) Apr 7, 2024
@elasticspoon elasticspoon changed the title feat(cli): prompt authenticated user on subsequent logins (#11004, #9329) feat(cli): prompt user for authentication on subsequent logins (#11004, #9329) Apr 7, 2024
@elasticspoon elasticspoon marked this pull request as ready for review April 7, 2024 00:23
@matifali matifali requested review from ammario and sreya April 7, 2024 12:10
@ammario ammario requested review from mafredri and removed request for sreya and ammario April 11, 2024 17:29
Copy link
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a few minor nits but LGTM otherwise, thanks for this PR!

cli/login.go Outdated Show resolved Hide resolved
cli/login.go Outdated Show resolved Hide resolved
cli/login.go Outdated Show resolved Hide resolved
cli/login.go Outdated Show resolved Hide resolved
@github-actions github-actions bot added the stale This issue is like stale bread. label Apr 23, 2024
cliui should return nil when returning cliui.Canceled (CTRL-C) and
return error others

fix nits
@github-actions github-actions bot removed the stale This issue is like stale bread. label Apr 26, 2024
I made an incorrect assumption, we can check if
a flag/env token is valid on login and create a new
token if the provided one is invalid.

However, on subsequent requests the invalid ENV token
will still take priority over the valid token stored
on disk. Therefore, this behavior does not work.
@github-actions github-actions bot added the stale This issue is like stale bread. label May 4, 2024
@matifali matifali removed the stale This issue is like stale bread. label May 5, 2024
@github-actions github-actions bot added the stale This issue is like stale bread. label May 13, 2024
@github-actions github-actions bot closed this May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community Pull Requests created by the community. stale This issue is like stale bread.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat(cli): print a warning if user is already authenticated
3 participants