keycloak-scim/securing_apps/topics/oidc/oidc-generic.adoc

198 lines
11 KiB
Text
Raw Normal View History

=== Other OpenID Connect Libraries
2016-04-18 19:10:32 +00:00
{project_name} can be secured by supplied adapters that are usually easier to use and provide better integration with {project_name}. However, if an adapter is not available for your programming language, framework, or platform you might opt to use a generic OpenID Connect Resource Provider (RP) library instead. This chapter describes details specific to {project_name} and does not contain specific protocol details. For more information see the https://openid.net/connect/[OpenID Connect specifications] and https://tools.ietf.org/html/rfc6749[OAuth2 specification].
2016-06-01 11:02:44 +00:00
2016-06-09 13:12:10 +00:00
==== Endpoints
2016-06-01 11:02:44 +00:00
2017-08-28 12:50:14 +00:00
The most important endpoint to understand is the `well-known` configuration endpoint. It lists endpoints and other configuration options relevant to the OpenID Connect implementation in {project_name}. The endpoint is:
2016-06-06 09:00:44 +00:00
....
2016-06-10 11:13:56 +00:00
/realms/{realm-name}/.well-known/openid-configuration
2016-06-06 09:00:44 +00:00
....
2017-08-28 12:50:14 +00:00
To obtain the full URL, add the base URL for {project_name} and replace `{realm-name}` with the name of your realm. For example:
2016-06-06 09:00:44 +00:00
2016-06-10 05:37:09 +00:00
$$http://localhost:8080/auth/realms/master/.well-known/openid-configuration$$
2016-06-06 09:00:44 +00:00
Some RP libraries retrieve all required endpoints from this endpoint, but for others you might need to list the endpoints individually.
2016-06-06 09:00:44 +00:00
2016-06-09 13:12:10 +00:00
===== Authorization Endpoint
2016-06-06 09:00:44 +00:00
....
2016-06-10 11:13:56 +00:00
/realms/{realm-name}/protocol/openid-connect/auth
2016-06-06 09:00:44 +00:00
....
The authorization endpoint performs authentication of the end-user. This is done by redirecting the user agent to this endpoint.
2016-06-06 09:00:44 +00:00
For more details see the https://openid.net/specs/openid-connect-core-1_0.html#AuthorizationEndpoint[Authorization Endpoint] section in the OpenID Connect specification.
2016-06-06 09:00:44 +00:00
2016-06-09 13:12:10 +00:00
===== Token Endpoint
2016-06-06 09:00:44 +00:00
....
2016-06-10 11:13:56 +00:00
/realms/{realm-name}/protocol/openid-connect/token
2016-06-06 09:00:44 +00:00
....
The token endpoint is used to obtain tokens. Tokens can either be obtained by exchanging an authorization code or by supplying credentials directly depending on what flow is used.
2016-06-06 09:00:44 +00:00
The token endpoint is also used to obtain new access tokens when they expire.
For more details see the https://openid.net/specs/openid-connect-core-1_0.html#TokenEndpoint[Token Endpoint] section in the OpenID Connect specification.
2016-06-06 09:00:44 +00:00
2016-06-09 13:12:10 +00:00
===== Userinfo Endpoint
2016-06-06 09:00:44 +00:00
....
2016-06-10 11:13:56 +00:00
/realms/{realm-name}/protocol/openid-connect/userinfo
2016-06-06 09:00:44 +00:00
....
The userinfo endpoint returns standard claims about the authenticated user, and is protected by a bearer token.
2016-06-06 09:00:44 +00:00
For more details see the https://openid.net/specs/openid-connect-core-1_0.html#UserInfo[Userinfo Endpoint] section in the OpenID Connect specification.
2016-06-06 09:00:44 +00:00
2016-06-09 13:12:10 +00:00
===== Logout Endpoint
2016-06-06 09:00:44 +00:00
....
2016-06-10 11:13:56 +00:00
/realms/{realm-name}/protocol/openid-connect/logout
2016-06-06 09:00:44 +00:00
....
The logout endpoint logs out the authenticated user.
2016-06-06 09:00:44 +00:00
The user agent can be redirected to the endpoint, in which case the active user session is logged out. Afterward the user agent is redirected back to the application.
2016-06-06 09:00:44 +00:00
The endpoint can also be invoked directly by the application. To invoke this endpoint directly the refresh token needs to be included as well as the credentials required to authenticate the client.
2016-06-06 09:00:44 +00:00
[[_certificate_endpoint]]
2016-06-09 13:12:10 +00:00
===== Certificate Endpoint
2016-06-06 09:00:44 +00:00
....
2016-06-10 11:13:56 +00:00
/realms/{realm-name}/protocol/openid-connect/certs
2016-06-06 09:00:44 +00:00
....
2017-08-28 12:50:14 +00:00
The certificate endpoint returns the public keys enabled by the realm, encoded as a JSON Web Key (JWK). Depending on the realm settings there can be one or more keys enabled for verifying tokens. For more information see the link:{adminguide_link}[{adminguide_name}] and the https://tools.ietf.org/html/rfc7517[JSON Web Key specification].
2016-06-06 09:00:44 +00:00
[[_token_introspection_endpoint]]
2016-06-09 13:12:10 +00:00
===== Introspection Endpoint
2016-06-06 09:00:44 +00:00
....
2016-06-10 11:13:56 +00:00
/realms/{realm-name}/protocol/openid-connect/token/introspect
2016-06-06 09:00:44 +00:00
....
2018-01-03 22:18:53 +00:00
The introspection endpoint is used to retrieve the active state of a token. In other words, you can use it to validate an access or refresh token.
It can only be invoked by confidential clients.
2016-06-06 09:00:44 +00:00
2018-01-03 22:18:53 +00:00
For more details on how to invoke on this endpoint, see https://tools.ietf.org/html/rfc7662[OAuth 2.0 Token Introspection specification].
2016-06-06 09:00:44 +00:00
2016-06-09 13:12:10 +00:00
===== Dynamic Client Registration Endpoint
2016-06-06 09:00:44 +00:00
....
2016-06-10 11:13:56 +00:00
/realms/{realm-name}/clients-registrations/openid-connect
2016-06-06 09:00:44 +00:00
....
The dynamic client registration endpoint is used to dynamically register clients.
2016-06-06 09:00:44 +00:00
2017-08-28 12:50:14 +00:00
For more details see the <<_client_registration,Client Registration chapter>> and the
2016-06-06 09:00:44 +00:00
https://openid.net/specs/openid-connect-registration-1_0.html[OpenID Connect Dynamic Client Registration specification].
2018-01-03 22:18:53 +00:00
==== Validating Access Tokens
If you need to manually validate access tokens issued by {project_name} you can invoke the <<_token_introspection_endpoint,Introspection Endpoint>>.
The downside to this approach is that you have to make a network invocation to the {project_name} server. This can be slow and possibily overload the
server if you have too many validation requests going on at the same time. {project_name} issued access tokens are https://tools.ietf.org/html/rfc7519[JSON Web Tokens (JWT)] digitally signed and encoded using https://www.rfc-editor.org/rfc/rfc7515.txt[JSON Web Signature (JWS)].
Because they are encoded in this way, this allows you to locally validate access tokens using the public key of the issuing realm. You can either hard code the
realm's public key in your validation code, or lookup and cache the public key using the <<_certificate_endpoint, certificate endpoint>> with the Key ID (KID) embedded within the
JWS. Depending what language you code in, there are a multitude of third party libraries out there that can help you with JWS validation.
2016-06-01 11:02:44 +00:00
2016-06-09 13:12:10 +00:00
==== Flows
2016-06-01 11:02:44 +00:00
2016-06-09 13:12:10 +00:00
===== Authorization Code
2016-06-06 09:00:44 +00:00
2017-08-28 12:50:14 +00:00
The Authorization Code flow redirects the user agent to {project_name}. Once the user has successfully authenticated with {project_name} an
2016-06-10 11:12:09 +00:00
Authorization Code is created and the user agent is redirected back to the application. The application then uses the authorization code along with its
2017-08-28 12:50:14 +00:00
credentials to obtain an Access Token, Refresh Token and ID Token from {project_name}.
2016-06-06 09:00:44 +00:00
The flow is targeted towards web applications, but is also recommended for native applications, including mobile applications, where it is possible to embed
a user agent.
For more details refer to the https://openid.net/specs/openid-connect-core-1_0.html#CodeFlowAuth[Authorization Code Flow] in the OpenID Connect specification.
2016-06-01 11:02:44 +00:00
2016-06-09 13:12:10 +00:00
===== Implicit
2016-06-01 11:02:44 +00:00
The Implicit flow redirects works similarly to the Authorization Code flow, but instead of returning an Authorization Code the Access Token and ID Token is
2016-06-06 09:00:44 +00:00
returned. This reduces the need for the extra invocation to exchange the Authorization Code for an Access Token. However, it does not include a Refresh
Token. This results in the need to either permit Access Tokens with a long expiration, which is problematic as it's very hard to invalidate these. Or
requires a new redirect to obtain new Access Token once the initial Access Token has expired. The Implicit flow is useful if the application only wants to
authenticate the user and deals with logout itself.
There's also a Hybrid flow where both the Access Token and an Authorization Code is returned.
One thing to note is that both the Implicit flow and Hybrid flow has potential security risks as the Access Token may be leaked through web server logs and
browser history. This is somewhat mitigated by using short expiration for Access Tokens.
For more details refer to the https://openid.net/specs/openid-connect-core-1_0.html#ImplicitFlowAuth[Implicit Flow] in the OpenID Connect specification.
2016-06-06 09:00:44 +00:00
[[_resource_owner_password_credentials_flow]]
2016-06-09 13:12:10 +00:00
===== Resource Owner Password Credentials
2016-06-01 11:02:44 +00:00
2017-08-28 12:50:14 +00:00
Resource Owner Password Credentials, referred to as Direct Grant in {project_name}, allows exchanging user credentials for tokens. It's not recommended
2016-06-06 09:00:44 +00:00
to use this flow unless you absolutely need to. Examples where this could be useful are legacy applications and command-line interfaces.
There are a number of limitations of using this flow, including:
* User credentials are exposed to the application
* Applications need login pages
* Application needs to be aware of the authentication scheme
* Changes to authentication flow requires changes to application
* No support for identity brokering or social login
* Flows are not supported (user self-registration, required actions, etc.)
2016-06-10 11:11:17 +00:00
For a client to be permitted to use the Resource Owner Password Credentials grant the client has to have the `Direct Access Grants Enabled` option enabled.
2016-06-08 04:10:13 +00:00
2016-06-06 09:00:44 +00:00
This flow is not included in OpenID Connect, but is a part of the OAuth 2.0 specification.
For more details refer to the https://tools.ietf.org/html/rfc6749#section-4.3[Resource Owner Password Credentials Grant] chapter in the OAuth 2.0 specification.
2016-06-09 13:12:10 +00:00
====== Example using CURL
2016-06-08 04:10:13 +00:00
The following example shows how to obtain an access token for a user in the realm `master` with username `user` and password `password`. The example is using
the confidential client `myclient`:
[source,bash]
----
curl \
-d "client_id=myclient" \
-d "client_secret=40cc097b-2a57-4c17-b36a-8fdf3fc2d578" \
-d "username=user" \
-d "password=password" \
-d "grant_type=password" \
"http://localhost:8080/auth/realms/master/protocol/openid-connect/token"
----
2016-06-09 13:12:10 +00:00
===== Client Credentials
2016-06-01 11:02:44 +00:00
2016-06-06 09:00:44 +00:00
Client Credentials is used when clients (applications and services) wants to obtain access on behalf of themselves rather than on behalf of a user. This can
for example be useful for background services that applies changes to the system in general rather than for a specific user.
2016-06-01 11:02:44 +00:00
2017-08-28 12:50:14 +00:00
{project_name} provides support for clients to authenticate either with a secret or with public/private keys.
2016-04-18 19:10:32 +00:00
2016-06-06 09:00:44 +00:00
This flow is not included in OpenID Connect, but is a part of the OAuth 2.0 specification.
For more details refer to the https://tools.ietf.org/html/rfc6749#section-4.4[Client Credentials Grant] chapter in the OAuth 2.0 specification.
2016-04-18 19:10:32 +00:00
2016-06-09 13:12:10 +00:00
==== Redirect URIs
2016-04-18 19:10:32 +00:00
2016-06-06 09:00:44 +00:00
When using the redirect based flows it's important to use valid redirect uris for your clients. The redirect uris should be as specific as possible. This
especially applies to client-side (public clients) applications. Failing to do so could result in:
2016-04-18 19:10:32 +00:00
2016-06-06 09:00:44 +00:00
* Open redirects - this can allow attackers to create spoof links that looks like they are coming from your domain
2017-08-28 12:50:14 +00:00
* Unauthorized entry - when users are already authenticated with {project_name} an attacker can use a public client where redirect uris have not be configured correctly to gain access by redirecting the user without the users knowledge
2016-06-06 09:00:44 +00:00
In production for web applications always use `https` for all redirect URIs. Do not allow redirects to http.
2016-04-18 19:10:32 +00:00
2016-06-06 09:00:44 +00:00
There's also a few special redirect URIs:
2016-04-18 19:10:32 +00:00
[[_installed_applications_url]]
2016-06-10 05:37:09 +00:00
`$$http://localhost$$`::
2016-06-06 09:00:44 +00:00
This redirect URI is useful for native applications and allows the native application to create a web server on a random port that can be used to obtain the
authorization code. This redirect uri allows any port.
2016-06-01 11:02:44 +00:00
[[_installed_applications_urn]]
2016-06-06 09:00:44 +00:00
`urn:ietf:wg:oauth:2.0:oob`::
2016-06-01 11:02:44 +00:00
2016-06-06 09:00:44 +00:00
If its not possible to start a web server in the client (or a browser is not available) it is possible to use the special `urn:ietf:wg:oauth:2.0:oob` redirect uri.
2017-08-28 12:50:14 +00:00
When this redirect uri is used {project_name} displays a page with the code in the title and in a box on the page.
2016-06-06 09:00:44 +00:00
The application can either detect that the browser title has changed, or the user can copy/paste the code manually to the application.
2016-06-07 06:14:38 +00:00
With this redirect uri it is also possible for a user to use a different device to obtain a code to paste back to the application.