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

Use access_token authentication method #31

Closed
craigharman opened this issue May 12, 2022 · 12 comments · Fixed by #54
Closed

Use access_token authentication method #31

craigharman opened this issue May 12, 2022 · 12 comments · Fixed by #54
Labels
enhancement New feature or request

Comments

@craigharman
Copy link
Contributor

Is your feature request related to a problem? Please describe.

You currently can't use the Directus access token method to authenticate to the API.
Ie. I share the "token" from the User > Admin Settings section and provide that in the Query String or the Authorization Bearer header.

Describe the solution you'd like

login should accept a {"token" OR { email: string, password: string }

<script setup lang="ts">
const { login } = useDirectusAuth();
const router = useRouter();

const onSubmit = async () => {
  try {
    await login({ token: "" });
  } catch (e) {}
};
</script>
@craigharman craigharman added the enhancement New feature or request label May 12, 2022
@craigharman
Copy link
Contributor Author

Just saw #16 so closing.

@vanling
Copy link

vanling commented May 12, 2022

I don't think #16 is fixing the problem you describe right?
Related #27

@Intevel
Copy link
Owner

Intevel commented May 12, 2022

@vanling You are right, this does not solve the problem. I will reopen this issue.

@Intevel Intevel reopened this May 12, 2022
@craigharman
Copy link
Contributor Author

I have done some more research and you are correct, this request is to use the stand-alone user token not JWT. You can append a query string ?access_token to each request but a way to add the Authorization Bearer header would be better IMO.

@rvmourik
Copy link

This indeed would be a nice feature to have an option to send the token through headers instead of the querystring.

@Intevel
Copy link
Owner

Intevel commented May 13, 2022

https://docs.directus.io/reference/authentication/#login

According to the Directus documentation, the login endpoint does not accept a token.

@vanling
Copy link

vanling commented May 13, 2022

@Intevel It's not so much for user authentication but for api access via an static token you can set per user.
https://docs.directus.io/reference/authentication/#access-tokens

Would love to be able to add a token in the nuxt config

  directus: {
    url: "https://url-to-directus-api.ext/",
    token: "5cc14101-11d3-4420-8067-a28750b4635f"
  },

And the idea is that the token would be used in an Authorization Bearer header for every request.

For example, i create a user in Directus called "api_application" and give that user a static access token.
image
I also create a Role for this user and this way I can use the api without making everything public.

Now i can make specific data available to the public for a single application.

extra info:
I am now using $fetch sometimes instead of nuxt-directus.

 const res = await $fetch(
    `${apiUrl}items/training?access_token=${token}&fields=${fields}&filter[slug][_eq]=${event.context.params.slug}`
  );

@Intevel
Copy link
Owner

Intevel commented May 14, 2022

@vanling I would say that we add to the DirectusQueryParams type the access_token and then you specify it via the method params.

At the setup of the module the options are merged with the public runtime config, the token would be revealed.

@Intevel
Copy link
Owner

Intevel commented May 14, 2022

Related to #27

@vanling
Copy link

vanling commented May 17, 2022

It would be ok to reveal the token to public if that's the choice of the developer. Would just need to be clear in the documentation that this would be the case.

I create tokens with restricted read/write access for different websites that use the same directus api/instance. This way I can block an application by removing a key and also see which application has written data into the api.

Directus SDK example: https://docs.directus.io/reference/sdk/#with-static-tokens

@Intevel
Copy link
Owner

Intevel commented May 18, 2022

@craigharman What do you think about this?

@craigharman
Copy link
Contributor Author

I agree it should be a developer choice.
For building static sites the token can be in a .env and will be built into the HTML/JS anyway.
For server rendered sites revealing the token would not be necessary and the token could be secure/hidden.

@vanling use case for the token will work although as per the directus documentation the long lived tokens are more designed for server to server communication where the token can be stored without exposing to client.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants