Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.

public final class LinkingOAuth2UserService

  1. Object
  2. LinkingOAuth2UserService

ImplementsOAuth2UserService<OAuth2UserRequest, OAuth2User>

Signs the user of an identity provider in as a user of the application’s own.

LinkingOAuth2UserService linking = new LinkingOAuth2UserService(identities, users);
linking.setCreateUsers(true);
http.oauth2Login(oauth2 -> oauth2
        .userService(linking)
        .oidcUserService(linking.oidc()));

Who the provider’s user is here is decided in this order:

  1. An identity – this provider, this subject – that is tied to a local user already signs in as that user. Nothing else about the provider’s answer is consulted: not the email, which may have changed since.

  2. Otherwise the provider must say the user’s email address is one it has verified. An address it does not vouch for is refused with EMAIL_NOT_VERIFIED, whether or not anybody here has it: tying an account to an address somebody merely typed at a provider would hand that account to them.

    A provider whose registration names a ClientRegistration.ProviderDetails.getUserEmailsUri – GitHub, whose user carries no such flag and often no address – is asked there instead, with the user’s access token: the address is the one the list marks both primary and verified, and whatever the user info said of an address is not consulted. No such entry, or a list that cannot be read (the user:email scope was not granted), is refused the same way.

  3. A verified address that is the name of a local user ties the identity to that user, and signs in as them.

  4. A verified address nobody here has makes a new local user of that name – when setCreateUsers allows it and the users are a UserDetailsManager – and is refused with ACCOUNT_NOT_FOUND otherwise.

The user that comes back is named after the local account and has its authorities, with the provider’s attributes beside them; a second factor and remember-me, which go by name, then apply to it exactly as they do to a sign-in with a password.

A new user has a password nobody knows: 256 random bits under an encoding id no encoder has. They sign in through the provider until they set one.

Fields

public static final String EMAIL_NOT_VERIFIED = "email_not_verified"The provider does not vouch for the user’s email address.
public static final String ACCOUNT_NOT_FOUND = "account_not_found"Nobody here has the address, and new users are not made.
public static final String ACCOUNT_UNAVAILABLE = "account_unavailable"The local user the identity is tied to cannot sign in.

Constructors

public LinkingOAuth2UserService(FederatedIdentityRepository identities, UserDetailsService users)

Methods

public void setCreateUsers(boolean createUsers)Whether a verified address nobody here has becomes a new local user.
public void setNewUserAuthorities(Collection<? extends GrantedAuthority> authorities)What a new user is granted; ROLE_USER unless set.
public void setEmailAttributes(String emailAttribute, String emailVerifiedAttribute)The attributes that hold the address and whether it is verified; email and email_verified unless set.
public void setOAuth2UserService(OAuth2UserService<OAuth2UserRequest, OAuth2User> service)What reads the user from a provider without OpenID Connect.
public void setOidcUserService(OAuth2UserService<OidcUserRequest, OidcUser> service)What reads the user from an OpenID Connect provider.
public OAuth2User loadUser(OAuth2UserRequest userRequest)The user.
public OAuth2UserService<OidcUserRequest, OidcUser> oidc()This service for a provider with OpenID Connect.

Inherited methods

Field details

EMAIL_NOT_VERIFIED

public static final String EMAIL_NOT_VERIFIED = "email_not_verified"
The provider does not vouch for the user’s email address.

ACCOUNT_NOT_FOUND

public static final String ACCOUNT_NOT_FOUND = "account_not_found"
Nobody here has the address, and new users are not made.

ACCOUNT_UNAVAILABLE

public static final String ACCOUNT_UNAVAILABLE = "account_unavailable"
The local user the identity is tied to cannot sign in.

Constructor details

LinkingOAuth2UserService

public LinkingOAuth2UserService(FederatedIdentityRepository identities, UserDetailsService users)

Parameters

identities FederatedIdentityRepository
Not documented.
users UserDetailsService
the application’s users; a UserDetailsManager to let new ones be made

Method details

setCreateUsers

public void setCreateUsers(boolean createUsers)
Whether a verified address nobody here has becomes a new local user. Off unless set.

setNewUserAuthorities

public void setNewUserAuthorities(Collection<? extends GrantedAuthority> authorities)
What a new user is granted; ROLE_USER unless set.

setEmailAttributes

public void setEmailAttributes(String emailAttribute, String emailVerifiedAttribute)
The attributes that hold the address and whether it is verified; email and email_verified unless set.

setOAuth2UserService

public void setOAuth2UserService(OAuth2UserService<OAuth2UserRequest, OAuth2User> service)
What reads the user from a provider without OpenID Connect.

setOidcUserService

public void setOidcUserService(OAuth2UserService<OidcUserRequest, OidcUser> service)
What reads the user from an OpenID Connect provider.

loadUser

public OAuth2User loadUser(OAuth2UserRequest userRequest)

The user.

  • OAuth2AuthenticationException: to refuse the sign-in

oidc

public OAuth2UserService<OidcUserRequest, OidcUser> oidc()
This service for a provider with OpenID Connect.