Keycloak 12.x#
Create a new Role at Keycloak, e.g.
demo-pam-authentication. (Assuming the server is athttps://keycloak.example.com)Create a new Client Scope, e.g.
pam_roles:- Protocol:
openid-connect - Display On Consent Screen:
OFF - Include in Token Scope:
ON - Mapper:
- Name: e.g.
pam roles - Mapper Type:
User Realm Role - Multivalued:
ON - Token Claim Name:
pam_roles(the name of the Client Scope) - Claim JSON Type:
String - Add to ID token:
OFF - Add to access token:
ON - Add to userinfo:
OFF
- Name: e.g.
- Scope:
- Effective Roles:
demo-pam-authentication(the name of the Role)
- Effective Roles:
- Protocol:
Create a new Keycloak Client:
- Client ID:
demo-pam(or whatever valid client name) - Enabled:
ON - Consent Required:
OFF - Client Protocol:
openid-connect - Access Type:
confidential - Standard Flow Enabled:
ON - Implicit Flow Enabled:
OFF - Direct Access Grants Enabled:
ON - Service Accounts Enabled:
OFF - Authorization Enabled:
OFF - Valid Redirect URIs:
urn:ietf:wg:oauth:2.0:oob - Fine Grain OpenID Connect Configuration:
- Access Token Signature Algorithm: e.g.
RS256(we need to put this in the config file later)
- Access Token Signature Algorithm: e.g.
- Client Scopes:
- Assigned Default Client Scopes:
pam_roles
- Assigned Default Client Scopes:
- Scope:
- Full Scope Allowed:
OFF - Effective Roles:
demo-pam-authentication
- Full Scope Allowed:
- Client ID:
Assign the role
demo-pam-authenticationto relevant users. A common practice is to assign the role to a Group, then make the relevant users join that group. Refer to Keycloak documents for the HOWTO.Download the precompiled binary from Github, e.g. as
/opt/pam-keycloak-oidc/pam-keycloak-oidc. In case the system is not amd64 or arm64, compile this golang application for the appropriate architecture.chmod +x /opt/pam-keycloak-oidc/pam-keycloak-oidcCreate the configuration file at the same directory, with the same filename as the binary plus a
.tmlfile extension. e.g.:vim /opt/pam-keycloak-oidc/pam-keycloak-oidc.tmlSet parameters at the configuration file:
# name of the dedicated OIDC client at Keycloak client-id="demo-pam" # the secret of the dedicated client client-secret="561319ba-700b-400a-8000-5ab5cd4ef3ab" # special callback address for no callback scenario redirect-url="urn:ietf:wg:oauth:2.0:oob" # OAuth2 scope to be requested, which contains the role information of a user scope="pam_roles" # name of the role to be matched, only Keycloak users who is assigned with this role could be accepted vpn-user-role="demo-pam-authentication" # retrieve from the meta-data at https://keycloak.example.com/auth/realms/demo-pam/.well-known/openid-configuration endpoint-auth-url="https://keycloak.example.com/auth/realms/demo-pam/protocol/openid-connect/auth" endpoint-token-url="https://keycloak.example.com/auth/realms/demo-pam/protocol/openid-connect/token" # 1:1 copy, to `fmt` substituion is required username-format="%s" # to be the same as the particular Keycloak client access-token-signing-method="RS256" # a key for XOR masking. treat it as a top secret xor-key="scmi" # use only otp code for auth otp-only=false