Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

oauth2-provider.md 4.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. ---
  2. date: "2019-04-19:44:00+01:00"
  3. title: "OAuth2 provider"
  4. slug: "oauth2-provider"
  5. weight: 41
  6. toc: false
  7. draft: false
  8. menu:
  9. sidebar:
  10. parent: "developers"
  11. name: "OAuth2 Provider"
  12. weight: 41
  13. identifier: "oauth2-provider"
  14. ---
  15. # OAuth2 provider
  16. **Table of Contents**
  17. {{< toc >}}
  18. Gitea supports acting as an OAuth2 provider to allow third party applications to access its resources with the user's consent. This feature is available since release 1.8.0.
  19. ## Endpoints
  20. | Endpoint | URL |
  21. | ------------------------ | ----------------------------------- |
  22. | OpenID Connect Discovery | `/.well-known/openid-configuration` |
  23. | Authorization Endpoint | `/login/oauth/authorize` |
  24. | Access Token Endpoint | `/login/oauth/access_token` |
  25. | OpenID Connect UserInfo | `/login/oauth/userinfo` |
  26. | JSON Web Key Set | `/login/oauth/keys` |
  27. ## Supported OAuth2 Grants
  28. At the moment Gitea only supports the [**Authorization Code Grant**](https://tools.ietf.org/html/rfc6749#section-1.3.1) standard with additional support of the following extensions:
  29. - [Proof Key for Code Exchange (PKCE)](https://tools.ietf.org/html/rfc7636)
  30. - [OpenID Connect (OIDC)](https://openid.net/specs/openid-connect-core-1_0.html#CodeFlowAuth)
  31. To use the Authorization Code Grant as a third party application it is required to register a new application via the "Settings" (`/user/settings/applications`) section of the settings.
  32. ## Scopes
  33. Currently Gitea does not support scopes (see [#4300](https://github.com/go-gitea/gitea/issues/4300)) and all third party applications will be granted access to all resources of the user and their organizations.
  34. ## Example
  35. **Note:** This example does not use PKCE.
  36. 1. Redirect to user to the authorization endpoint in order to get their consent for accessing the resources:
  37. ```curl
  38. https://[YOUR-GITEA-URL]/login/oauth/authorize?client_id=CLIENT_ID&redirect_uri=REDIRECT_URI& response_type=code&state=STATE
  39. ```
  40. The `CLIENT_ID` can be obtained by registering an application in the settings. The `STATE` is a random string that will be send back to your application after the user authorizes. The `state` parameter is optional but should be used to prevent CSRF attacks.
  41. ![Authorization Page](/authorize.png)
  42. The user will now be asked to authorize your application. If they authorize it, the user will be redirected to the `REDIRECT_URL`, for example:
  43. ```curl
  44. https://[REDIRECT_URI]?code=RETURNED_CODE&state=STATE
  45. ```
  46. 2. Using the provided `code` from the redirect, you can request a new application and refresh token. The access token endpoints accepts POST requests with `application/json` and `application/x-www-form-urlencoded` body, for example:
  47. ```curl
  48. POST https://[YOUR-GITEA-URL]/login/oauth/access_token
  49. ```
  50. ```json
  51. {
  52. "client_id": "YOUR_CLIENT_ID",
  53. "client_secret": "YOUR_CLIENT_SECRET",
  54. "code": "RETURNED_CODE",
  55. "grant_type": "authorization_code",
  56. "redirect_uri": "REDIRECT_URI"
  57. }
  58. ```
  59. Response:
  60. ```json
  61. {
  62. "access_token": "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJnbnQiOjIsInR0IjowLCJleHAiOjE1NTUxNzk5MTIsImlhdCI6MTU1NTE3NjMxMn0.0-iFsAwBtxuckA0sNZ6QpBQmywVPz129u75vOM7wPJecw5wqGyBkmstfJHAjEOqrAf_V5Z-1QYeCh_Cz4RiKug",
  63. "token_type": "bearer",
  64. "expires_in": 3600,
  65. "refresh_token": "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJnbnQiOjIsInR0IjoxLCJjbnQiOjEsImV4cCI6MTU1NzgwNDMxMiwiaWF0IjoxNTU1MTc2MzEyfQ.S_HZQBy4q9r5SEzNGNIoFClT43HPNDbUdHH-GYNYYdkRfft6XptJBkUQscZsGxOW975Yk6RbgtGvq1nkEcklOw"
  66. }
  67. ```
  68. The `CLIENT_SECRET` is the unique secret code generated for this application. Please note that the secret will only be visible after you created/registered the application with Gitea and cannot be recovered. If you lose the secret you must regenerate the secret via the application's settings.
  69. The `REDIRECT_URI` in the `access_token` request must match the `REDIRECT_URI` in the `authorize` request.
  70. 3. Use the `access_token` to make [API requests](https://docs.gitea.io/en-us/api-usage#oauth2) to access the user's resources.