def validatedUserIdentity()

in play-v29/src/main/scala/com/gu/googleauth/auth.scala [227:263]


  def validatedUserIdentity(config: GoogleAuthConfig)
        (implicit request: RequestHeader, context: ExecutionContext, ws: WSClient): Future[UserIdentity] = {

    Future.fromTry(config.antiForgeryChecker.verifyToken(request)).flatMap(_ => discoveryDocument()).flatMap { dd =>
      val code = request.queryString("code")
      ws.url(dd.token_endpoint).post {
        Map(
          "code" -> code,
          "client_id" -> Seq(config.clientId),
          "client_secret" -> Seq(config.clientSecret),
          "redirect_uri" -> Seq(config.redirectUrl),
          "grant_type" -> Seq("authorization_code")
        )
      }.flatMap { response =>
        googleResponse(response) { json =>
          val token = Token.fromJson(json)
          val jwt = token.jwt
          checkDomains(config.domains, jwt.claims)
          ws.url(dd.userinfo_endpoint)
            .withHttpHeaders("Authorization" -> s"Bearer ${token.access_token}")
            .get().map { response =>
            googleResponse(response) { json =>
              val userInfo = UserInfo.fromJson(json)
              UserIdentity(
                sub = jwt.claims.sub,
                email = jwt.claims.email,
                firstName = userInfo.given_name,
                lastName = userInfo.family_name,
                exp = jwt.claims.exp,
                avatarUrl = userInfo.picture
              )
            }
          }
        }
      }
    }
  }