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

Expose Auth Token to API user for CLI Client #742

Open
PeopleMakeCulture opened this issue Oct 30, 2024 · 8 comments
Open

Expose Auth Token to API user for CLI Client #742

PeopleMakeCulture opened this issue Oct 30, 2024 · 8 comments
Assignees

Comments

@PeopleMakeCulture
Copy link
Collaborator

PeopleMakeCulture commented Oct 30, 2024

From @shreddd in Slack

We need to address a programmatic access use case from JGI and I wanted to understand how best to support this. The simple (hopefully) thing we would like short term is being able to quickly get the access token (bearer token) that an API user can use in a CLI client. Right now it requires a somewhat hacky approach to introspect the outgoing call or looking at the Curl dump of the call.

Related to #404

@PeopleMakeCulture
Copy link
Collaborator Author

Additional Context:
you can use the /users/token endpoint with grant type "password" and a username and password. the response will contain an access token of token type "bearer" and an expiration datetime:

    return {
        "access_token": access_token,
        "token_type": "bearer",
        "expires": ACCESS_TOKEN_EXPIRES.model_dump(),
    }

@PeopleMakeCulture PeopleMakeCulture changed the title Auth Expose Auth Token to API user for CLI Client Oct 30, 2024
@PeopleMakeCulture
Copy link
Collaborator Author

@shreddd could you clarify if they would want to log in via an ORCiD, or with the username/password flow, or both?

@dwinston
Copy link
Collaborator

If you POST /token with grant_type=client_credentials and client_id={KNOWN_ORCID_JWT}, then the response will be {"access_token": ..., "token_type": "bearer", "expires": ...} as above. So, this could work as is until we perhaps have a more streamlined credentials flow without "sites" etc.

@dwinston
Copy link
Collaborator

(waiting on use case confirmation/clarification)

@shreddd
Copy link
Collaborator

shreddd commented Oct 30, 2024

I think this is OK but seems like an extra step? The goal is to see if we can provide a token to the user for easy use. Poking at the code a bit I noticed that once logged into swagger we use a cookie to talk to the server - wondering if we could take advantage of this. Will follow up on Slack and post updates here.

Given that we already have a cookie for logged in user, is it possible to use that cookie to get and display the token? This could potentially live in the custom HTML? One option - would it be possible to display the access token from: https://github.com/microbiomedata/nmdc-runtime/blob/main/nmdc_runtime/api/main.py#L483

@shreddd
Copy link
Collaborator

shreddd commented Oct 31, 2024

We want this for both. Basically we always want to give the users an easy way to look at their token if they need it.

@PeopleMakeCulture

@shreddd could you clarify if they would want to log in via an ORCiD, or with the username/password flow, or both?

@PeopleMakeCulture
Copy link
Collaborator Author

@shreddd Could you explain how you're thinking about programmatically accessing the cookie to access the token?

One thing we could do is refactor the code below into its own endpoint, so that a user can POST their JWT and GET a bearer token. Is that what you had in mind?

    if user_id_token:
        # get bearer token
        rv = requests.post(
            url=f"{BASE_URL_EXTERNAL}/token",
            data={
                "client_id": user_id_token,
                "client_secret": "",
                "grant_type": "client_credentials",
            },
            headers={
                "Content-type": "application/x-www-form-urlencoded",
                "Accept": "application/json",
            },
        )
        if rv.status_code != 200:
            rv.reason = rv.text
            rv.raise_for_status()
        access_token = rv.json()["access_token"]

@PeopleMakeCulture PeopleMakeCulture self-assigned this Nov 1, 2024
@dwinston
Copy link
Collaborator

possibility: add bearer token info to GET /users/me response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: On stage
Development

No branches or pull requests

3 participants