boblack_zocker
boblack_zockerβ€’3y ago

Get all roles from every project into one access token

Hello everyone I have a question. Currently when I get a token from the oidc client everything works fine. With the ClientId i get the roles from the project. Now my question is if its possible to get all roles of the user from every project that is currently available in the project. I have about 7 projects and I need all users roles for every project. If I want to do that I need 7 different access tokens but I only want to have 1. Is something like that possible?
81 Replies
FFO
FFOβ€’3y ago
Hm good question Currently we do not have that ability read made. However if you want to be create you could create an action and with the help of our http module you could call the data through the management API (the internal managment API does not expose this yet)
FFO
FFOβ€’3y ago
I.e with this you can tweak the token content https://zitadel.com/docs/apis/actions/complement-token
ZITADEL Docs
This flow is executed during the creation of tokens and token introspection.
FFO
FFOβ€’3y ago
This is the http module for actions https://zitadel.com/docs/apis/actions/modules
ZITADEL Docs
ZITADEL provides the following modules.
boblack_zocker
boblack_zockerOPβ€’3y ago
hey @FFO thanks for your message. I have to admit I don't even know where to begin making this action and what exactly I have to doπŸ˜… . As much as I understand it I have to call an action in my backend and tweak the token content (I don't know what tweak means). Is that right?
boblack_zocker
boblack_zockerOPβ€’3y ago
is this the action you've meant?
No description
FFO
FFOβ€’3y ago
Yes, you can basically run JS code at certain points of zitadel I.e when we create tokens
FFO
FFOβ€’3y ago
We just started collecting examples here https://github.com/zitadel/actions/tree/main/examples
GitHub
actions/examples at main Β· zitadel/actions
ZITADEL Actions - Easy extensibility with custom code. Think GitHub Actions in an Identity System. - actions/examples at main Β· zitadel/actions
boblack_zocker
boblack_zockerOPβ€’3y ago
Can you tell me how I tweak the token content with all roles? What should my script have to do?
FFO
FFOβ€’3y ago
Currently its a little verbose πŸ™ˆ You would need to fetch all the roles from a call to the management API with the current users id set /users/grants/_search
FFO
FFOβ€’3y ago
No description
FFO
FFOβ€’3y ago
this could be done using the http module and then you can assert the values to the tokens
boblack_zocker
boblack_zockerOPβ€’3y ago
thats the ListMyUserGrants right?
FFO
FFOβ€’3y ago
on the auth api yes but that is only scoped to a single user when you have the users token in the actions you currently do not have the users token
boblack_zocker
boblack_zockerOPβ€’3y ago
the thing is when i do that i get the result i want: every role the user has on every project. but i think the performance would really struggle if i do it like this in the backend cause i would have to call it every time my API gets called.
No description
boblack_zocker
boblack_zockerOPβ€’3y ago
i actually only want all roles from a single user but on different projects. I think you misunderstood me at the beginning could that be? the only thing is that i want this information in the users claim in the access token so i dont have to call it every time with ListMyUserGrants. because currently i only get the roles of one project.
FFO
FFOβ€’3y ago
You can easy cache the response for a moment and in most place you should get the response quickly from our end No I understood that. My point was more that in actions you could call the management API with the userID as filter to get all roles from a user across all projects. The reason to use the mgmt API is only that you can hardcode the access token into the action πŸ™Š
boblack_zocker
boblack_zockerOPβ€’3y ago
that makes sense to me. The only thing is i don't know where to start with the action πŸ˜‚
FFO
FFOβ€’3y ago
Got it πŸ˜„ I can try and spend some time tomorrow on an example 😁 In the meantime the query you make could be cached to reduce the burden of latency πŸ˜„
boblack_zocker
boblack_zockerOPβ€’3y ago
is it something like that? thats from the docs
No description
FFO
FFOβ€’3y ago
Out of curiosity with gRPC how high is the latency on your end?
FFO
FFOβ€’3y ago
yes https://github.com/zitadel/actions/blob/main/examples/add_user_grant.js this is an example that uses the action internal mgmt methods to add grants to a user. but we are always evolving this so we will open up more things internally
GitHub
actions/add_user_grant.js at main Β· zitadel/actions
ZITADEL Actions - Easy extensibility with custom code. Think GitHub Actions in an Identity System. - actions/add_user_grant.js at main Β· zitadel/actions
boblack_zocker
boblack_zockerOPβ€’3y ago
uf i would have to test it can't tell you rigth away
FFO
FFOβ€’3y ago
no problem, i was just curious
boblack_zocker
boblack_zockerOPβ€’3y ago
well in my example i have to setClaim() in the access token is that right?
boblack_zocker
boblack_zockerOPβ€’3y ago
well this is how you set the grants in the access token but how do i get them in the first place πŸ˜…
No description
FFO
FFOβ€’3y ago
yeah the getting the roles is basically a POST with JS to our management API
boblack_zocker
boblack_zockerOPβ€’3y ago
this one right?
FFO
FFOβ€’3y ago
yes Pro Tip, create a new zitadel instance to tinker around πŸ˜„
boblack_zocker
boblack_zockerOPβ€’3y ago
the userid is in ctx.v1.user ?
FFO
FFOβ€’3y ago
ctx.v1.user.id
boblack_zocker
boblack_zockerOPβ€’3y ago
aha okey
FFO
FFOβ€’3y ago
thats what you want to pass as filter
boblack_zocker
boblack_zockerOPβ€’3y ago
its starts to make sense even more now i think i know where i can begin now to do the action πŸ˜‚
FFO
FFOβ€’3y ago
in your case the flow should be https://zitadel.com/docs/apis/actions/complement-token with the trigger pre-access-token if you want to have a JWT access token with the roles
ZITADEL Docs
This flow is executed during the creation of tokens and token introspection.
boblack_zocker
boblack_zockerOPβ€’3y ago
yes that makes sense well i'm gonna try it and tell u the result later on πŸ‘ . Really appreciate your help thanks!
FFO
FFOβ€’3y ago
happy to help
boblack_zocker
boblack_zockerOPβ€’3y ago
one more questionπŸ˜… . Is that the only way to make an action? can i make the action in a standalone javascript file so i have a debugger and interpreter.
No description
FFO
FFOβ€’3y ago
ATM yes, but thats currently a field we are planning to improve πŸ˜„ Btw. make sure that the name and the function name lines up there would be terraform to apply action but developing them needs more love
boblack_zocker
boblack_zockerOPβ€’3y ago
i was just trying something out there πŸ˜… πŸ˜‚ its a bit hard to think without a interpreter hahaha
FFO
FFOβ€’3y ago
agreed you can create the code in your ide and paste it πŸ˜„
boblack_zocker
boblack_zockerOPβ€’3y ago
already did hehe
No description
boblack_zocker
boblack_zockerOPβ€’3y ago
url right as i did or does it need the full path?
FFO
FFOβ€’3y ago
full path
boblack_zocker
boblack_zockerOPβ€’3y ago
how can i see if it worked or not?
boblack_zocker
boblack_zockerOPβ€’3y ago
I made this script and zitadel gives me no feedback if it works or not. the action is stated as active but nothing happens
No description
boblack_zocker
boblack_zockerOPβ€’3y ago
its says its active but it doesn't look like its working
No description
boblack_zocker
boblack_zockerOPβ€’3y ago
it looks like my function never gets called but i'm not sure
FFO
FFOβ€’3y ago
sorry was out for lunch What you need to do is not write on the api.userGrants but instead on api.v1.claims.setClaim btw. are you self-hosted or cloud? because in self-hosted you can also use our log method to printout stuff to stdout (there is a logger coming for the cloud as well)
boblack_zocker
boblack_zockerOPβ€’3y ago
no problem I was out too. I'm currently self-hosted how does that work?
FFO
FFOβ€’3y ago
let logger = require("zitadel/log")

logger.log("Hello World")
let logger = require("zitadel/log")

logger.log("Hello World")
boblack_zocker
boblack_zockerOPβ€’3y ago
i tried to do api.v1.userinfo.setClaim('year', 2023) as a test but it didnt get added
FFO
FFOβ€’3y ago
api.v1.userinfo.setClaim is for id_tokens
boblack_zocker
boblack_zockerOPβ€’3y ago
oh i see
FFO
FFOβ€’3y ago
api.v1.claims.setClaim is for access tokens
boblack_zocker
boblack_zockerOPβ€’3y ago
so this should work?
No description
FFO
FFOβ€’3y ago
i think you need to do a post to fetch data but first glance looks good
boblack_zocker
boblack_zockerOPβ€’3y ago
jep that true
FFO
FFOβ€’3y ago
just FYI @livio and I a hunting a bug in the setClaim code
boblack_zocker
boblack_zockerOPβ€’3y ago
is it even right if i set the parameters like in my code? it seems to take a string and any
No description
FFO
FFOβ€’3y ago
ehm, a string as key and any as body
boblack_zocker
boblack_zockerOPβ€’3y ago
is that enough to get my function executed?
No description
FFO
FFOβ€’3y ago
make sure the name of the function is the same as your func ....
boblack_zocker
boblack_zockerOPβ€’3y ago
i looked at my logs i dont get a hello world or anything else
No description
boblack_zocker
boblack_zockerOPβ€’3y ago
name of the function is also the same
boblack_zocker
boblack_zockerOPβ€’3y ago
No description
boblack_zocker
boblack_zockerOPβ€’3y ago
I searched my logs in the docker container. But there is nothing. is there a own log file in the system that outputs the logs?
boblack_zocker
boblack_zockerOPβ€’3y ago
ZITADEL Docs
Quotas is an enterprise feature that is relevant if you want to host ZITADEL as a service.
boblack_zocker
boblack_zockerOPβ€’3y ago
GitHub
zitadel/defaults.yaml at main Β· zitadel/zitadel
ZITADEL - The best of Auth0 and Keycloak combined. Built for the serverless era. - zitadel/defaults.yaml at main Β· zitadel/zitadel
No description
boblack_zocker
boblack_zockerOPβ€’3y ago
do i need that stuff? and if yes where do i have to add it
FFO
FFOβ€’3y ago
hm no that was just introduced to have option with zitadel to log all acces request (i.e reading data) hm if it is not logging it is either not hit or you hit the bug @livio are looking for
boblack_zocker
boblack_zockerOPβ€’3y ago
The actions i did should be hit as much as i can tell because everything was done right on the console site. It could really be that nothing gets logged. It was really frustrating yesterday searching for this logsπŸ˜‚ . Is it right that those logs must show up in "docker compose logs"? I also tried to test actions made from your sample (https://github.com/zitadel/actions/blob/main/examples/post_auth_log.js). Here even the syntax and everything should be okay but still no logs there. Also none that the process failed or something.
No description
No description
No description
No description
boblack_zocker
boblack_zockerOPβ€’3y ago
just for my understanding. When does the action gets executed exactly? When the user calls "/oauth/v2/token"?
FFO
FFOβ€’3y ago
Yes exactly, to me it looks like your hit a bug there, let me work this out with @livio
boblack_zocker
boblack_zockerOPβ€’3y ago
Yes sure. Pls let me know if there is a updateπŸ‘
FFO
FFOβ€’3y ago
Will do So, we just poked around a little. Can you try the same with zitadel 2.20.0 we improved the logger to output error better
boblack_zocker
boblack_zockerOPβ€’3y ago
Is that the latest version?
FFO
FFOβ€’3y ago
yes
boblack_zocker
boblack_zockerOPβ€’3y ago
i get now console logs πŸ‘ . but kinda not helpful logs but still better then nothing 😁 . it looks like i get the whole http request logged. i dont know why beacause i dont log that anywhere. starts with: time="2023-02-17T13:25:06Z" level=info msg="log record emitted" caller="/home/runner/work/zitadel/zitadel/internal/logstore/emitters/stdout/stdout.go:19" record="{"logDate":"2023-02-17T13:25:06.892322614Z","protocol":1,"requestUrl":"/oauth/v2/token\ ........ and goes on
FFO
FFOβ€’3y ago
hm can you share more of the logs?
boblack_zocker
boblack_zockerOPβ€’3y ago
yes but can i send it private to you?
FFO
FFOβ€’3y ago
surely, send a DM

Did you find this page helpful?