In a Spring Boot application, I have an OAuth2 Authorization/Resource servers. Based on this and Spring Security, I have secured my Spring MVC REST API endpoints.
In addition to this, I'd like to add authentication to my REST endpoints based on 3rd party OAuth providers like Twitter, Facebook, Google.
In my application I have two entities - User
and SocialUser
. SocialUser
represents user profile in social networks. User
can have 0-* associated SocialUsers
. Right now I can authenticate a user in Twitter and after that I'm creating two records in my database - User and SocialUser. SocialUser contains access/refresh tokens issued by Twitter and some other profile information from this social network.
Right now I don't know how to link this User created from social network with my existing authentication\authorization flow. For this user I'd like to create my own(by my own OAuth2 authorization server) accessToken and provide it to the client.
Also, this user doesn't have username, password and email in his User
entity. And also I don't know how to manually create my own access Token and send it to the client for future API calls.
I found some example:
@Inject
private TokenEndpoint tokenEndpoint;
public String createAccessToken(User user) {
HashMap<String, String> parameters = new HashMap<String, String>();
parameters.put("client_id", "appid");
parameters.put("client_secret", "myOAuthSecret");
parameters.put("grant_type", "password");
parameters.put("password", user.getPassword());
parameters.put("scope", "read write");
parameters.put("username", user.getUsername());
// principal ??
return tokenEndpoint.getAccessToken(principal, parameters);
}
but I don't know how to create Principal
based on my User
entity and also I'm not sure that this is a correct way to go.
So, the main question is - how to manually generate this new token through my own OAuth server for this new user?
Please advise me how can it be correctly implemented. Thanks.
UPDATED:
I have added ProviderSignInController
to my application and able right now to perform full OAuth dance with Twitter. Also, I have implemented my own Neo4jConnectionRepository
and Neo4jUsersConnectionRepository
because I use Neo4j as a primary database.
@Bean
public ProviderSignInController providerSignInController() {
return new ProviderSignInController(socialAuthenticationServiceLocator, usersConnectionRepository, new SignInAdapter() {
@Override
public String signIn(String userId, Connection<?> connection, NativeWebRequest request) {
System.out.println("User ID: " + userId + " social display name: " + connection.getDisplayName());
return null;
}
});
}
So far, everything works good.
The one question is - how to authenticate/authorize User
through my own OAuth2 Authorization server in the SignInAdapter.signIn
method ?
I think I need to create OAuth2Authentication
object for this user and put it into Security context.. Am I right ? If so, could you please show me an example how can this be implemented ?
SignInAdapter
? – OratorianI'm moved away from own OAuth2 server to JWT
? Did you move away from OAuth ? – Chaconne