Closed
Description
Hi there, are tokens related to a session cookie specific. How can I create a persistent token, to login as user, without having an open session?
Follow this steps:
- Login Coder
coder login URL
> Welcome to Coder, coder! You're authenticated.
-
Create a token for ~6 years
coder tokens create --lifetime 50000h
-
Check tokens
coder tokens list
ID NAME LAST USED EXPIRES AT CREATED AT
**** ***** 0001-01-01T00:00:00Z 2029-03-06T03:32:19Z 2023-06-22T19:32:19Z
-
Logout Coder
coder logout
-
Login again
coder login --token **** URL
> Welcome to Coder, coder! You're authenticated.
-
Logout Again
coder logout
-
Login again
coder login --token **** URL
You are signed out or your session has expired. Please sign in again to continue.
Try logging in using 'coder login <url>'.
-
Login without token, to create a new session
coder login URL
-
Chek tokens
coder tokens list
No tokens found.
ID NAME LAST USED EXPIRES AT CREATED AT`
Activity
ericpaulsen commentedon Jun 23, 2023
hi @francisco-mata - thank you for submitting this issue.
i've reproduced the behavior on our latest version,
0.24.1
.it's unclear to me whether this is the intended behavior, or a bug. i'm following up with engineering and will reach out when I learn more.update: i've confirmed with product that this is a bug.
francisco-mata commentedon Jun 23, 2023
Thanks for the quick response @ericpaulsen. I'm glad to know its a bug and not the intended behaviour.
Emyrk commentedon Jun 26, 2023
The issue is that
/logout
deletes the API token being used:coder/coderd/userauth.go
Line 203 in 1406838
We should not delete the token being used if the token was created via the
token create
command.Emyrk commentedon Jun 26, 2023
What I will implement is
/logout
will fail withtokens
. I will print a message to usecoder token delete
if the user tries to use/logout
.francisco-mata commentedon Jun 26, 2023
@Emyrk but the token should survive to an user logout, don't they? If you create a token, is because you want to log as user using the token instead of credentials. And this token will life till lifetime duration expire or user wants to delete the token. I dont see why it should failed if user logouts or the token will be delete if user logouts.
spikecurtis commentedon Jun 27, 2023
@Emyrk I suggest that instead,
coder login --token
should create a new session token with the usual session token semantics & expiry. We should not persist the token given on the command line as their session.Emyrk commentedon Jun 27, 2023
@spikecurtis would that not be a bit confusing?
This works because "token" is a global level flag.
coder/cli/root.go
Lines 303 to 309 in 1406838
So all commands allow the
--token
flag, which makes sense for one off commands. I don't thinkcoder ls --token
should create a new session.I do see value in having a command to use a provided token. Would it not be confusing if
coder login --token=<token>
didn't use the provided token? I am guessing we would just use the token to make an api key rather than redirecting to the UI.--
@francisco-mata
When you hit the
/logout
route, the server deletes that session token. It does this because it is the other half of/login
.What you are trying to do I believe, is have the cli delete the credentials on the local disk. I think we make another command for that, or add some flag to do a client-side "logout".
So basically
/logout
is the other half of/login
. It should undo what/login
does. If the token comes fromtoken create
, then/logout
doesn't make sense. I think it would be better to throw an error than to do something potentially unexpected. For your use case, another command or flag can be implemented with documented behavior.spikecurtis commentedon Jun 28, 2023
Yeah, I think it makes sense for
--token
to use that token for the command being invoked. We shouldn't be storing that token for later use.In the case of
coder login --token
, we still use the token, during that command, to authenticate against the API, but then as you say, we generate a new token, and only the new token is persisted, in the same way we would if you logged in via the UI. So, you used the token to "login", but once you are logged in, it's a different, session token that gets persisted. When you "logout" the persisted token is invalidated, but the token you used oncoder login
is still valid.This gives us the following properties:
logout
is the other half oflogin
, and undoes it--token
uses the token for the command being executed, but does not persist the tokenfrancisco-mata commentedon Jun 28, 2023
Sorry I got confused a bit, just to get a clear idea. If I create a token with the command
coder token create --lifetime 24h
, it will be deleted only 24 hours after creation or if I manually runcoder token delete
. For all the other cases token will persist and will be vaild to use, even tho my session is already logout.Emyrk commentedon Jun 28, 2023
Ah I see. That does make sense.
Yes
What do you mean "for all ther other cases"? @spikecurtis is suggesting doing
coder login --token <token>
and then doingcoder logout
will not delete<token>
.5 remaining items