OAuth authentication / “Connect with pretix”¶
In addition to static tokens, pretix supports OAuth2-based authentication starting with pretix 1.16. This allows you to put a “Connect with pretix” button into your website or tool that allows the user to easily set up a connection between the two systems.
If you haven’t worked with OAuth before, have a look at the OAuth2 Simplified tutorial.
Registering an application¶
To use OAuth, you need to register your application with the pretix instance you want to connect to. In order to do this, log in to your pretix account and go to your user settings. Click on “Authorized applications” first and then on “Manage your own apps”. From there, you can “Create a new application”.
You should fill in a descriptive name of your application that allows users to recognize who you are. You also need to give a list of fully-qualified URLs that users will be redirected to after a successful authorization. After you pressed “Save”, you will be presented with a client ID and a client secret. Please note them down and treat the client secret like a password; it should not become available to your users.
Getting an access token¶
Using the code
value you obtained above and your client ID, you can now request an access token that actually gives
access to the API. The token
endpoint expects you to authenticate using HTTP Basic authentication using your client
ID as a username and your client secret as a password. You are also required to again supply the same redirect_uri
parameter that you used for the authorization.
- POST /api/v1/oauth/token¶
Request a new access token
Example request:
POST /api/v1/oauth/token HTTP/1.1 Host: pretix.eu Accept: application/json, text/javascript Authorization: Basic bHNMaTBoTkwwdms1M21FZFlqTkp4SFVuMVBjTzFSNndWZzgxZExOVDplSmpzZVA0UjJMN0hMcjBiS0p1b3BmbnJtT2cyY3NDeTdYaFVVZ0FoalhUU0NhZHhRTjk3cVNvMkpPaXlWTFpQOEozaTVQd1FVdFIwNUNycG5ac2Z0bXJjdmNTbkZ1SkFmb2ZsUTdZUDRpSjZNTWFYTHIwQ0FpNlhIRFJjV1Awcg== grant_type=authorization_code&code=eYBBf8gmeD4E01HLoj0XflqO4Lg3Cw&redirect_uri=https://pretalx.com
Example response:
HTTP/1.1 200 OK Vary: Accept Content-Type: application/json { "access_token": "i3ytqTSRWsKp16fqjekHXa4tdM4qNC", "expires_in": 86400, "token_type": "Bearer", "scope": "read write", "refresh_token": "XBK0r8z4A4TTeR9LyMUyU2AM5rqpXp" }
- Status Codes:
200 OK – no error
401 Unauthorized – Authentication failure
As you can see, you receive two types of tokens: One “access token”, and one “refresh token”. The access token is valid for a day and can be used to actually access the API. The refresh token does not have an expiration date and can be used to obtain a new access_token after a day, so you should make sure to store the access token safely if you need long-term access.
Using the API with an access token¶
You can supply a valid access token as a Bearer
-type token in the Authorization
header to get API access.
GET /api/v1/organizers/ HTTP/1.1
Host: pretix.eu
Authorization: Bearer i3ytqTSRWsKp16fqjekHXa4tdM4qNC
Refreshing an access token¶
You can obtain a new access token using your refresh token any time. This can be done using the same token
endpoint
used to obtain the first access token above, but with a different set of parameters:
POST /api/v1/oauth/token HTTP/1.1
Host: pretix.eu
Accept: application/json, text/javascript
Authorization: Basic bHNMaTBoTkwwdms1M21FZFlqTkp4SFVuMVBjTzFSNndWZzgxZExOVDplSmpzZVA0UjJMN0hMcjBiS0p1b3BmbnJtT2cyY3NDeTdYaFVVZ0FoalhUU0NhZHhRTjk3cVNvMkpPaXlWTFpQOEozaTVQd1FVdFIwNUNycG5ac2Z0bXJjdmNTbkZ1SkFmb2ZsUTdZUDRpSjZNTWFYTHIwQ0FpNlhIRFJjV1Awcg==
grant_type=refresh_token&refresh_token=XBK0r8z4A4TTeR9LyMUyU2AM5rqpXp
The previous access token will instantly become invalid.
Revoking a token¶
If you don’t need a token any more or if you believe it may have been compromised, you can use the revoke_token
endpoint to revoke it.
- GET /api/v1/oauth/revoke_token¶
Revoke an access or refresh token. If you revoke an access token, you can still create a new one using the refresh token. If you revoke a refresh token, the connected access token will also be revoked.
Example request:
POST /api/v1/oauth/revoke_token HTTP/1.1 Host: pretix.eu Accept: application/json, text/javascript Authorization: Basic bHNMaTBoTkwwdms1M21FZFlqTkp4SFVuMVBjTzFSNndWZzgxZExOVDplSmpzZVA0UjJMN0hMcjBiS0p1b3BmbnJtT2cyY3NDeTdYaFVVZ0FoalhUU0NhZHhRTjk3cVNvMkpPaXlWTFpQOEozaTVQd1FVdFIwNUNycG5ac2Z0bXJjdmNTbkZ1SkFmb2ZsUTdZUDRpSjZNTWFYTHIwQ0FpNlhIRFJjV1Awcg== token=XBK0r8z4A4TTeR9LyMUyU2AM5rqpXp
Example response:
HTTP/1.1 200 OK Vary: Accept Content-Type: application/json
- Status Codes:
200 OK – no error
401 Unauthorized – Authentication failure
If you want to revoke your client secret, you can generate a new one in the list of your managed applications in the pretix user interface.
Fetching the user profile¶
If you need the user’s meta data, you can fetch it here:
- GET /api/v1/me¶
Returns the profile of the authenticated user
Example request:
GET /api/v1/me HTTP/1.1 Host: pretix.eu Accept: application/json, text/javascript Authorization: Bearer i3ytqTSRWsKp16fqjekHXa4tdM4qNC
Example response:
HTTP/1.1 200 OK Vary: Accept Content-Type: application/json { "email": "admin@localhost", "fullname": "John Doe", "locale": "de", "is_staff": false, "timezone": "Europe/Berlin" }
- Status Codes:
200 OK – no error
401 Unauthorized – Authentication failure