安全社交连接提供商

时间:2013-10-15 15:16:13

标签: scala gmail playframework-2.1 securesocial

我是Play框架的新手,因此我是安全社交的新手。我必须在项目中实现Google身份验证,但我不知道我应该如何与gmail建立联系。我所拥有的是一个扩展身份的帐户类:

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

然后我创建一个帐户集合,迭代它们并识别用户想要连接的提供程序。

 for(account <- accounts){
      if(account.identityId.providerId == service){
        //Sends account info to securesocial module
          success = true
      }
  }

我应该如何调用安全社交API才能连接到服务,在这种情况下是Gmail?

2 个答案:

答案 0 :(得分:1)

  1. 为用户提供选择界面。
  2. 使用Google API Console注册您的应用。创建2个id用于测试,1个用于prod。
  3. 创建一个重定向到Google服务器的按钮。
  4. URL看起来像这样:

    https://accounts.google.com/o/oauth2/auth?response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email&client_id=your_client_id&redirect_uri=your_redirect_uri

    1. 重定向到上述链接后,用户会授予应用访问Google帐户的权限。
    2. 谷歌将用户重定向到您的网站,但它包含code令牌。
    3. 您需要3种主要方法:

         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]
          )
      

      现在您有了一个响应,将其映射到您自己的自定义用户实现。

      最后一步是:

      • 如果用户已存在(存储用户的GoogleID并按其搜索,请不要为此目的使用电子邮件)

      • 如果用户不存在,请添加它们,询问其他详细信息等。

      • 在这两种情况下,都可以通过为用户创建会话来对用户进行身份验证。

答案 1 :(得分:1)

您无需亲自与Google联系。 SecureSocial为您处理所有身份验证流程。你需要的是:

1)添加指向Google的链接,以便用户点击该链接并启动身份验证流程 2)实施UserService,以便SecureSocial可以将用户保存在您的数据库中。 3)在play.plugins文件中注册Google插件。 4)使用SecuredAction而不是Play内置的Action保护您的操作。

如果未经过身份验证,SecuredAction会拦截请求并将用户重定向到登录页面。

检查模块附带的示例应用程序,它们提供了您可以使用的基本框架,并可以扩展以构建您的应用程序。

相关问题