REST API? Add users via CLI + Acquire an access token #270
-
Main goal is to identify a config file + CLI commands for supporting a test:
Presumably a mocked I don't have much time presently to give Rauthy a try, just raising this Q in advance :)
Recently I was advised about using an API key to approach this:
I had a quick browse/search on the docs but I didn't see anything regarding an API key? There's not much context about the API key, is there a REST API to use it with? With Ory Hydra their API has routes under Use-case scenarioTechnical referenceInitial interest was for replacing a mocked auth service for this test case (shell script based): function __should_login_successfully_with() {
local AUTH_METHOD=${1}
# These values are the auth credentials checked against the Caddy `/userinfo` endpoint:
local USER_ACCOUNT='[email protected]'
local ACCESS_TOKEN='DMS_YWNjZXNzX3Rva2Vu'
__verify_auth_with_imap
__verify_auth_with_smtp
}
# Dovecot direct auth verification via IMAP:
function __verify_auth_with_imap() {
# NOTE: Include the `--verbose` option if you're troubleshooting and want to see the protocol exchange messages
# NOTE: `--user username:password` is valid for testing `PLAIN` auth mechanism, but you should prefer swaks instead.
_run_in_container curl --silent \
--login-options "AUTH=${AUTH_METHOD}" --oauth2-bearer "${ACCESS_TOKEN}" --user "${USER_ACCOUNT}" \
--url 'imap://localhost:143' -X 'LOGOUT'
__dovecot_logs_should_verify_success
} which is presently uses a hard-coded access token as shown above against this mocked
The software makes a request to the auth service curl http://auth.example.test/userinfo -H 'Authorization: Bearer DMS_YWNjZXNzX3Rva2Vu' and expects the JSON response to have an email attribute with a value that matches the login username to authenticate successfully: {
"email": "[email protected]",
"email_verified": true
} I was curious if Rauthy can be configured with shell commands for the test-suite to replace that mocked service?:
The 2nd concern for acquiring an access token probably doesn't make much sense outside of a test? So the mocked endpoint with static access token is probably the right approach (and less burden to other project maintainers that don't really grok the actual auth process which is more complicated). Earlier attemptWhen I explored what was out there (such as Rauthy and Ory Hydra) this was all new to me and docs were not always straight-forward 😅 I recall having success with registering a client to Hydra where I could easily get the access token with credentials for that client, but that was only useful for a service to authenticate to another with, rather than the next step of getting the Rauthy from what I recall was focused on a DB + GUI interaction with an admin user, so I think I was more swayed towards Ory Kratos + Hydra due to their broader docs + CLI/API and config. I had also looked into Authelia and Authentik, but recall some friction on both of those (Authelia was a bit interesting as I think it could configure users via a file or LDAP source). Ory Hydra did offer some alternative flows that did not require a browser interaction, but I ran out of time before having success with those via curl (I think I hit some issues there and their docs are a bit messy / outdated or oriented towards their paid SaaS). |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
There are a few ways, how you can achieve all of this, but I need some more information. I guess you want to run tests against Rauthy in automated test pipelines and stuff, right?
Unfortunately, that's correct, they are not in the book yet. As I am getting closer to v1.0.0, one of my next TODO's is to update the docs with all the latest features that I added recently. API Keys are not yet in the book, but you should have no trouble getting them up and running by just using the Admin UI. Everything is documented and you even get Create a new API Key
So, either access it via the internal port, or just expose it with the config: If exposed, you can just click the link in the UI, if not, the URL would be something like
but on the
So, now to your goals. 1. SolutionCreate your whole instance like you need it, with everything set up. The version, again, depends on your environment. If you have some test postgres running somewhere, just connect to this instance inside the pipelines, so you data never changes. Otherwise, use the SQLite version of course. You could then provide your database file, which should be
You could do this dynamically with the API Key, or when you use the DB migration procedure from above, you can work with a static user as well, once it has been set up. Via API, you can do a POST on
That's actually the "tricky" part. You could of course just use the The code flow is done via a browser, like you mentioned already. To prevent Login CSRF attacks, you actually need a session in the backend in the rauthy/rauthy-main/tests/handler_auth.rs Line 72 in eec43bf You can extract the cookies like so: rauthy/rauthy-main/tests/common.rs Line 141 in eec43bf This is the only tricky part about it. Afterwards, you can do the POST, which will "redirect" you to your callback endpoint. You can then use the
That is simple. Just provide the
API yes, like mentioned above. Adding things by file is not supported. You would use the above approach with creating the test database just like you need it in advance and then just use that file inside your pipelines.
No. I planned to add something to inspect the token responses after you configured everything, so you can get a preview of what the final token for a given client + user would look like, but even when that comes, these tokens will not be valid and expire immediately for security reasons. 2. SolutionIf you are just using this for testing and nothing else, you could actually do the same that I do with Rauthy itself for its integration tests.
Also, the To start in test mode, you would however need to start the binary directly, or modify the docker run cmd and append 3. SolutionI need to double check something, but it might be possible with the |
Beta Was this translation helpful? Give feedback.
-
3. SolutionThis is actually working. I just checked it while testing the latest release.
The default scopes is the important one here. Since you cannot provide the requested scope during the This is way simpler than using the |
Beta Was this translation helpful? Give feedback.
3. Solution
This is actually working. I just checked it while testing the latest release.
If you just need the
email
andemail_verififed
from the userinfo, there is an even easier way.You should not do this in your pipeline, but I am using the
password
flow for a clientemail
in the Allowed and Default ScopesThe default scopes is the important one here. Since you cannot provide the requested scope during the
password
flow, Rauthy will always use the set default. This will make the login a lot easier! This flow does not use the default login CSRF protection.You can simply do a POST request only on the
/token
endpoint withContent-Type: application/x-www-form-ur…