From: Glen Youngjin Kim Date: Thu, 1 Sep 2016 09:21:10 +0000 (+0900) Subject: Add Google OAuth2 Provider X-Git-Tag: 1.2.0+RC3~119 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=97674df8c0734a6fcff8e037e6f1f97db80ab2a0;p=platform%2Fupstream%2Fiotivity.git Add Google OAuth2 Provider Change-Id: Ib2062996074621b80da1acbb1e7fd3b8f3dbf687 Signed-off-by: Glen Youngjin Kim Reviewed-on: https://gerrit.iotivity.org/gerrit/11271 Tested-by: jenkins-iotivity Reviewed-by: Jee Hyeok Kim (cherry picked from commit 2c4f2d3be6cc402bb7aec8d5d8c2939b07c3d935) Reviewed-on: https://gerrit.iotivity.org/gerrit/11985 Reviewed-by: Yeonghun Nam Tested-by: Jee Hyeok Kim --- diff --git a/cloud/account/Google.jar b/cloud/account/Google.jar new file mode 100644 index 0000000..cb3697c Binary files /dev/null and b/cloud/account/Google.jar differ diff --git a/cloud/account/src/main/java/org/iotivity/cloud/accountserver/Constants.java b/cloud/account/src/main/java/org/iotivity/cloud/accountserver/Constants.java index 1fc6d88..564ad40 100644 --- a/cloud/account/src/main/java/org/iotivity/cloud/accountserver/Constants.java +++ b/cloud/account/src/main/java/org/iotivity/cloud/accountserver/Constants.java @@ -236,4 +236,6 @@ public class Constants extends OICConstants { public static final String GITHUB = "Github"; public static final String SAMSUNG = "Samsung"; + + public static final String GOOGLE = "Google"; } diff --git a/cloud/account/src/main/java/org/iotivity/cloud/accountserver/oauth/Google.java b/cloud/account/src/main/java/org/iotivity/cloud/accountserver/oauth/Google.java new file mode 100644 index 0000000..72577c7 --- /dev/null +++ b/cloud/account/src/main/java/org/iotivity/cloud/accountserver/oauth/Google.java @@ -0,0 +1,195 @@ +/* + * //****************************************************************** + * // + * // Copyright 2016 Samsung Electronics All Rights Reserved. + * // + * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + * // + * // Licensed under the Apache License, Version 2.0 (the "License"); + * // you may not use this file except in compliance with the License. + * // You may obtain a copy of the License at + * // + * // http://www.apache.org/licenses/LICENSE-2.0 + * // + * // Unless required by applicable law or agreed to in writing, software + * // distributed under the License is distributed on an "AS IS" BASIS, + * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * // See the License for the specific language governing permissions and + * // limitations under the License. + * // + * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + */ +package org.iotivity.cloud.accountserver.oauth; + +import java.util.HashMap; + +import org.apache.oltu.oauth2.client.OAuthClient; +import org.apache.oltu.oauth2.client.URLConnectionClient; +import org.apache.oltu.oauth2.client.request.OAuthBearerClientRequest; +import org.apache.oltu.oauth2.client.request.OAuthClientRequest; +import org.apache.oltu.oauth2.client.response.OAuthAccessTokenResponse; +import org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse; +import org.apache.oltu.oauth2.client.response.OAuthResourceResponse; +import org.apache.oltu.oauth2.common.OAuth; +import org.apache.oltu.oauth2.common.OAuthProviderType; +import org.apache.oltu.oauth2.common.exception.OAuthProblemException; +import org.apache.oltu.oauth2.common.exception.OAuthSystemException; +import org.apache.oltu.oauth2.common.message.types.GrantType; +import org.iotivity.cloud.accountserver.db.TokenTable; +import org.iotivity.cloud.accountserver.db.UserTable; +import org.iotivity.cloud.base.exception.ServerException.InternalServerErrorException; +import org.iotivity.cloud.util.JSONUtil; +import org.iotivity.cloud.util.Log;; + +/** + * + * This class provides a sample to make .jar that communicates with OAuth2 + * provider. + * + */ +public class Google implements OAuthProvider { + + // do not use 'client_id' and 'secret' variables. + // should use values that are obtained from github. + final static private String client_id = "447649044559-f9r5sl6op3kkk0312u384o4g6hhucje1.apps.googleusercontent.com"; + final static private String secret = "LyTe5_EQkv8-v9Zbq20PSCLR"; + final static private String redirect_url = "http://www.example.com/oauth2callback"; + final static private String resource_url = "https://www.googleapis.com/userinfo/v2/me"; + + @Override + public TokenTable requestAccessTokenInfo(String authCode, Object options) { + + TokenTable tokenInfo = new TokenTable(); + + if (authCode == null) { + + Log.w("authCode is null!"); + return tokenInfo; + } + + try { + + OAuthClientRequest request = OAuthClientRequest + .tokenProvider(OAuthProviderType.GOOGLE) + .setGrantType(GrantType.AUTHORIZATION_CODE) + .setClientId(client_id).setClientSecret(secret) + .setCode(authCode).setRedirectURI(redirect_url) + .buildBodyMessage(); + + OAuthClient oauthClient = new OAuthClient( + new URLConnectionClient()); + + OAuthAccessTokenResponse oauthResponse = null; + Class cl = OAuthJSONAccessTokenResponse.class; + + oauthResponse = oauthClient.accessToken(request, cl); + + Log.d("OAuth response: " + oauthResponse.getBody()); + + tokenInfo.setAccesstoken(oauthResponse.getAccessToken()); + tokenInfo.setRefreshtoken(oauthResponse.getRefreshToken()); + tokenInfo.setExpiredtime(oauthResponse.getExpiresIn()); + + } catch (OAuthSystemException | OAuthProblemException e) { + + e.printStackTrace(); + throw new InternalServerErrorException( + "OAuth provider(Google) error"); + } + + return tokenInfo; + } + + @Override + public TokenTable requestRefreshTokenInfo(String refreshToken) { + + TokenTable tokenInfo = new TokenTable(); + + if (refreshToken == null) { + + Log.w("refreshToken is null!"); + return tokenInfo; + } + + try { + + OAuthClientRequest request = OAuthClientRequest + .tokenProvider(OAuthProviderType.GOOGLE) + .setGrantType(GrantType.REFRESH_TOKEN) + .setClientId(client_id).setClientSecret(secret) + .setRefreshToken(refreshToken).buildBodyMessage(); + + OAuthClient oauthClient = new OAuthClient( + new URLConnectionClient()); + + OAuthAccessTokenResponse oauthResponse = null; + Class cl = OAuthJSONAccessTokenResponse.class; + + oauthResponse = oauthClient.accessToken(request, cl); + + Log.d("OAuth response: " + oauthResponse.getBody()); + + tokenInfo.setAccesstoken(oauthResponse.getAccessToken()); + // Google provides refreshToken in one time. + tokenInfo.setRefreshtoken(refreshToken); + tokenInfo.setExpiredtime(oauthResponse.getExpiresIn()); + + } catch (OAuthSystemException | OAuthProblemException e) { + + e.printStackTrace(); + throw new InternalServerErrorException( + "OAuth provider(Google) error"); + } + + return tokenInfo; + } + + @Override + public UserTable requestGetUserInfo(String accessToken, Object options) { + + UserTable userInfo = new UserTable(); + + if (accessToken == null) { + Log.w("accessToken is null!"); + return userInfo; + } + + String response = null; + + try { + + OAuthClientRequest request = new OAuthBearerClientRequest( + resource_url).setAccessToken(accessToken) + .buildQueryMessage(); + + OAuthClient oAuthClient = new OAuthClient( + new URLConnectionClient()); + OAuthResourceResponse resourceResponse = oAuthClient.resource( + request, OAuth.HttpMethod.GET, OAuthResourceResponse.class); + + response = resourceResponse.getBody(); + Log.d("response: " + response); + + } catch (OAuthSystemException | OAuthProblemException e) { + e.printStackTrace(); + throw new InternalServerErrorException( + "OAuth provider(Github) error"); + } + + String userIdKey = "name"; + + JSONUtil> util = new JSONUtil<>(); + HashMap parsedData = util.parseJSON(response, + HashMap.class); + + if (parsedData == null) { + Log.d("parsedData is null!"); + return userInfo; + } + + String userId = parsedData.get(userIdKey); + userInfo.setUserid(userId); + + return userInfo; + } +}