Reputation: 334
I'm new to Play framework, therefore I'm new to secure social. I have to implement Google authentication within a project but I don't know how exactly am I supposed to connect with gmail. What I have is an account class that extends Identity like this:
case class Account(
identityId: IdentityId,
firstName: String,
lastName: String,
fullName: String,
email: Option[String],
avatarUrl: Option[String],
authMethod: AuthenticationMethod,
oAuth1Info: Option[OAuth1Info] = None,
oAuth2Info: Option[OAuth2Info] = None,
passwordInfo: Option[PasswordInfo] = None
)extends Identity
Then I create a collection of Accounts, iterate them and identify the provider the user wants to connect.
for(account <- accounts){
if(account.identityId.providerId == service){
//Sends account info to securesocial module
success = true
}
}
How am I supposed to call the secure social API in order to connect to the service, in this case Gmail?
Upvotes: 0
Views: 303
Reputation: 1403
You don't have to connect to Google yourself. SecureSocial handles all the authentication flow for you. What you need is:
1) Add a link to Google so the user clicks there and starts the authentication flow 2) Implement a UserService so SecureSocial can save users in your DB. 3) Register the Google plugin in play.plugins file. 4) Protect your actions using SecuredAction instead of Play's built in Action.
SecuredAction intercepts the requests and redirects the user to the login page if he is not authenticated.
Check the sample apps that come with the module, they provide a basic skeleton you can use and extend to build your app.
Upvotes: 1
Reputation: 28511
The URL looks something like this:
code
token.You need 3 main methods:
case class GoogleTokenResponse(
access_token: String,
token_type: String,
expires_in: String,
id_token: String
);
def getAccessToken: GoogleTokenResponse
// this is an HTTP request to https://accounts.google.com:443?code=the_code_param
def getUserData: HttpResponse
// this will get the user data from www.googleapis.com
// it needs the OAuth2 access_token obtained above.
val req = url("https://www.googleapis.com") / "oauth2" / "v2" / "userinfo" <<? ("alt" -> "json") <<?
Map(OAuthParams.access_token -> token.access_token); // this is a databinder dispatch call.
// this is how a Google profile response looks like.
case class GoogleUserResponse(
val id: String,
val name: String,
val given_name: String,
val family_name: String,
val verified_email: Boolean,
val email: String,
val locale: Option[String],
val link: Option[String],
val hd: Option[String]
)
Now you have a response, map it to your own custom user implementation.
The last stage is:
If the user already exists(store the GoogleID of the user and search by it, DO NOT USE EMAIL for this purpose)
If the user doesn't exist, add them, ask for additional details, etc.
Upvotes: 1