|
| 1 | +package services; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import controllers.Application; |
| 5 | +import models.User; |
| 6 | +import org.apache.http.client.methods.HttpGet; |
| 7 | +import org.apache.http.client.utils.URIBuilder; |
| 8 | +import play.libs.F; |
| 9 | +import play.libs.WS; |
| 10 | +import play.mvc.Http; |
| 11 | + |
| 12 | + |
| 13 | +import java.net.MalformedURLException; |
| 14 | +import java.net.URISyntaxException; |
| 15 | +import java.util.HashMap; |
| 16 | +import java.util.Map; |
| 17 | + |
| 18 | +/** |
| 19 | + * @author yannig |
| 20 | + * <p/> |
| 21 | + * Date: 2014-06-24 3:56 PM |
| 22 | + */ |
| 23 | +public class Google { |
| 24 | + |
| 25 | + private String authEndpoint; |
| 26 | + private String tokenEndpoint; |
| 27 | + private String userEndpoint; |
| 28 | + |
| 29 | + |
| 30 | + //<---------------------------------------------------------------------------> |
| 31 | + //- Authentication OAuth call |
| 32 | + //<---------------------------------------------------------------------------> |
| 33 | + |
| 34 | + /** |
| 35 | + * <p/> |
| 36 | + * Document infos example: |
| 37 | + * <p/> |
| 38 | + * { |
| 39 | + * "issuer": "accounts.google.com", |
| 40 | + * "authorization_endpoint": "https://accounts.google.com/o/oauth2/auth", |
| 41 | + * "token_endpoint": "https://accounts.google.com/o/oauth2/token", |
| 42 | + * "userinfo_endpoint": "https://www.googleapis.com/plus/v1/people/me/openIdConnect", |
| 43 | + * "revocation_endpoint": "https://accounts.google.com/o/oauth2/revoke", |
| 44 | + * "jwks_uri": "https://www.googleapis.com/oauth2/v2/certs", |
| 45 | + * "response_types_supported": [ |
| 46 | + * "code", |
| 47 | + * "token", |
| 48 | + * "id_token", |
| 49 | + * "code token", |
| 50 | + * "code id_token", |
| 51 | + * "token id_token", |
| 52 | + * "code token id_token", |
| 53 | + * "none" |
| 54 | + * ], |
| 55 | + * "subject_types_supported": [ |
| 56 | + * "public" |
| 57 | + * ], |
| 58 | + * "id_token_alg_values_supported": [ |
| 59 | + * "RS256" |
| 60 | + * ], |
| 61 | + * "token_endpoint_auth_methods_supported": [ |
| 62 | + * "client_secret_post" |
| 63 | + * ] |
| 64 | + * } |
| 65 | + */ |
| 66 | + public F.Promise<JsonNode> getEndpoint() { |
| 67 | + |
| 68 | + F.Promise<WS.Response> document = WS.url( Application.conf( "oauth.google.document" ) ).get(); |
| 69 | + |
| 70 | + return document.map( new F.Function<WS.Response, JsonNode>() { |
| 71 | + @Override |
| 72 | + public JsonNode apply( WS.Response response ) throws Throwable { |
| 73 | + |
| 74 | + JsonNode jsonNode = response.asJson(); |
| 75 | + authEndpoint = jsonNode.get( "authorization_endpoint" ).asText(); |
| 76 | + tokenEndpoint = jsonNode.get( "token_endpoint" ).asText(); |
| 77 | + userEndpoint = jsonNode.get( "userinfo_endpoint" ).asText(); |
| 78 | + |
| 79 | + return jsonNode; |
| 80 | + } |
| 81 | + } ); |
| 82 | + |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + */ |
| 87 | + public String authenticationUrl( String token ) throws URISyntaxException, MalformedURLException { |
| 88 | + HttpGet endpoint = new HttpGet( authEndpoint ); |
| 89 | + URIBuilder builder = new URIBuilder( endpoint.getURI() ); |
| 90 | + builder.addParameter( "client_id", Application.conf("oauth.google.clientID") ) |
| 91 | + .addParameter( "response_type", Application.conf( "oauth.google.responseType" ) ) |
| 92 | + .addParameter( "scope", Application.conf( "oauth.google.scope" ) ) |
| 93 | + .addParameter( "redirect_uri", Application.conf( "oauth.verifyUri" ) ) |
| 94 | + .addParameter( "state", token ); |
| 95 | + |
| 96 | + return builder.build().toURL().toString(); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * <p/> |
| 101 | + * Token info JSON example: |
| 102 | + * { |
| 103 | + * "access_token": "ya29.1.AADtN_UoDvNkY-6HzKuXNyMSgJjrZa6ToKBVaLGeDnRLHMjM1SiOw7c3Ci7Nsk20vuVyaJ4", |
| 104 | + * "token_type": "Bearer", |
| 105 | + * "expires_in": 3600, |
| 106 | + * "id_token": "eyJhbGciOiJSUzI1NiIsImtpZCI6IjAwYzVmZjE3NzQ4OTAyYmY3MzI5YmIyNDRiZmViZmZlMDg2MGY3M2IifQ.eyJpc3MiOiJhY2NvdW50cy5nb29nbGUuY29tIiwic3ViIjoiMTA0NTczNjQyMjcyNzQ0MjE5OTkxIiwiYXpwIjoiMjE2OTg2Mjc5MDUwLWQycTRpNW1ybGVpbmRnOGV2NWF0ZDZ0OTlvczBtZXY3LmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwiZW1haWwiOiJ5Y2hhcnRvaXNAeXNkZXYuZnIiLCJhdF9oYXNoIjoiVjNLa0pFMl9RV1pEM1RQS1kyT2dOdyIsImVtYWlsX3ZlcmlmaWVkIjp0cnVlLCJhdWQiOiIyMTY5ODYyNzkwNTAtZDJxNGk1bXJsZWluZGc4ZXY1YXRkNnQ5OW9zMG1ldjcuYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLCJoZCI6InlzZGV2LmZyIiwiaWF0IjoxMzk4ODUwMDYyLCJleHAiOjEzOTg4NTM5NjJ9.ezk20Jf28gcsRPu4bRqec4Y4QIMeM89NAFXYCnnQ4vg836RB0ooMoNb9kgvhRXy2OcZrpsaDe1wsPBtg9_9oA4-PcncGeIUsM5mVcQ5qlqW5AADBhNT2L5gLTXQIlc-3aNVDt4XjZ794Q_QW_DDtrajsrVi2Alop1OUQf_gxBy8" |
| 107 | + * } |
| 108 | + */ |
| 109 | + public F.Promise<JsonNode> getTokenInfo( Http.Request request ) { |
| 110 | + String code = request.queryString().get( "code" )[0]; |
| 111 | + |
| 112 | + Map<String, String> postParams = new HashMap<>(); |
| 113 | + postParams.put( "code", code ); |
| 114 | + postParams.put( "client_id", Application.conf( "oauth.google.clientID" ) ); |
| 115 | + postParams.put( "client_secret", Application.conf( "oauth.google.secret" ) ); |
| 116 | + postParams.put( "grant_type", Application.conf( "oauth.google.grant.type" ) ); |
| 117 | + postParams.put( "redirect_uri", Application.conf( "oauth.verifyUri" ) ); |
| 118 | + |
| 119 | + F.Promise<WS.Response> tokenRequest = WS.url( tokenEndpoint ) |
| 120 | + .setHeader( "Content-Type", "application/x-www-form-urlencoded" ) |
| 121 | + .post( Application.postParams( postParams ) ); |
| 122 | + |
| 123 | + return tokenRequest.map( new F.Function<WS.Response, JsonNode>() { |
| 124 | + @Override |
| 125 | + public JsonNode apply( WS.Response response ) throws Throwable { |
| 126 | + return response.asJson(); |
| 127 | + } |
| 128 | + } ); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * <p/> |
| 133 | + * User info JSON example: |
| 134 | + * { |
| 135 | + * "kind": "plus#personOpenIdConnect", |
| 136 | + * "sub": "1087545455544219991", |
| 137 | + * "name": "Yannick Chartois", |
| 138 | + * "given_name": "Yannick", |
| 139 | + * "family_name": "Chartois", |
| 140 | + * "picture": "https:https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=50", |
| 141 | + |
| 142 | + * "email_verified": "true", |
| 143 | + * "locale": "fr", |
| 144 | + * "hd": "ysdev.fr" |
| 145 | + * } |
| 146 | + */ |
| 147 | + public F.Promise<JsonNode> getUserInfo( JsonNode tokenInfo ) { |
| 148 | + F.Promise<WS.Response> userInfos = WS.url( userEndpoint ) |
| 149 | + .setQueryParameter( "access_token", getToken( tokenInfo ) ) |
| 150 | + .get(); |
| 151 | + |
| 152 | + return userInfos.map( new F.Function<WS.Response, JsonNode>() { |
| 153 | + @Override |
| 154 | + public JsonNode apply( WS.Response response ) throws Throwable { |
| 155 | + return response.asJson(); |
| 156 | + } |
| 157 | + } ); |
| 158 | + } |
| 159 | + |
| 160 | + //<---------------------------------------------------------------------------> |
| 161 | + //- Convenient method to retrieve information |
| 162 | + //<---------------------------------------------------------------------------> |
| 163 | + |
| 164 | + public User getUser( JsonNode userInfo ) { |
| 165 | + |
| 166 | + User user = new User(); |
| 167 | + user.firstName = userInfo.get( "given_name" ).asText(); |
| 168 | + user.lastName = userInfo.get( "family_name" ).asText(); |
| 169 | + user.email = userInfo.get( "email" ).asText(); |
| 170 | + |
| 171 | + return user; |
| 172 | + } |
| 173 | + |
| 174 | + |
| 175 | + public String getEmail( JsonNode userInfo ) { |
| 176 | + return userInfo.get( "email" ).asText(); |
| 177 | + } |
| 178 | + |
| 179 | + public String getToken( JsonNode tokenInfo ) { |
| 180 | + return tokenInfo.get( "access_token" ).asText(); |
| 181 | + } |
| 182 | +} |
0 commit comments