From 2c4f2d3be6cc402bb7aec8d5d8c2939b07c3d935 Mon Sep 17 00:00:00 2001 From: Glen Youngjin Kim Date: Thu, 1 Sep 2016 18:21:10 +0900 Subject: [PATCH] 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 --- cloud/account/Google.jar | Bin 0 -> 3220 bytes .../iotivity/cloud/accountserver/Constants.java | 2 + .../iotivity/cloud/accountserver/oauth/Google.java | 195 +++++++++++++++++++++ 3 files changed, 197 insertions(+) create mode 100644 cloud/account/Google.jar create mode 100644 cloud/account/src/main/java/org/iotivity/cloud/accountserver/oauth/Google.java diff --git a/cloud/account/Google.jar b/cloud/account/Google.jar new file mode 100644 index 0000000000000000000000000000000000000000..cb3697cd7fac7cbbd6f3ef12188a8dffb1d84260 GIT binary patch literal 3220 zcmbW3XE+-Q7sn$;QKQs|y+^E2JJe2;2&uhF(HON8B1Vf|N{y5vY891HJsv3LbuLSZu95LlnmZ&$;f@Eh3u41+(+#dnakoz|UaJ{!FA-{1Q7E z{l|p@zx%faJiVq1n2nsOy*r9ccS|Op`uqD-Cx%j&!M>pp+hdap*&W+Gg0`p8fu8Dl zdBUA%SCqGpUw!Y`J}-#;HsM78NOQecL>9eI3Zv~I3T$1R{p&TV*Az#Wih64b-;Cqg zR$kE`x;8Wans;a;=`pW15ZeN}16Oo6YP**z;6zTV6|pNi*5FaK-D?#IIE>dCCUvEt z#Qor#$R%^C=)W$ld1?s~a9IrGs)nZwvxzhQaB-gqC99%l625P5*(NzT8DVP68Q{VR zF)<=K)uXRoy_%dY?~v=<+%P4x<+w0kz~rx#&B0S||Fi-zP*@@@B71wk_i5UMY+9b8 zVer_Idvn+N;a~Ee17#aS5kG9Xj%sJ7>OH7 zqXL?1_IFBeo&S;wGaDFf{q!M??Li2rSP+&|_h>h59rZ*MB@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oo-=$+sS0((o#CB!<@i~_9&qWC7^!%z3&CM+=kwI0wdro^jn82> zxwk?;ewRf8oyK3>qmbgbZ={A-EEAwKbwrAD0#I!QQ7EIgn z){~tH){M-@9e9Z078&?{IREY^9nd^K|n zk(;pNvfOd6a>cFv;iJi$%oHIJdf)30jCbz>$77OrsG`y)gSgqEnS_>Kv9<+i`kuBP zv5k;lAJ{3n8ZHcx;5{R)b350g$bCVO0hNH66?c-0+p&g{bLQ)vu4_z%qEXCZ@Anfw z@ITGw+mV)gt}x64gh=tV!!XjXos7)2#@|wnd_-w5HN#l^C@XNuej4@CP-z;7GdxUrnJ@y z7LRLVa-UEQI?TRnV$*Y=xvi$ow3tRc-iW!9)r<|glu)^^-#@KWoR7Tb3jMHEs2lUl zdIJlNvaN~+>9oeq=@HKNe|mtU)};xf!(ZH8GvbWazp_hM(21@aX|jVZqz0O*oP8w9 zxeB*!gIxsAlfh?;(k_|#CDOX-gmB>^lb3}8eTdS9?ilrWSJ`c+m`nO?lsn~abMe@d z_Ux+m!VP?kqRust(DL&;&e3r)AB{DHmYy3=?`<3EWHlvLIluXCTGfy_*dDj-SXGxU z!!%mcwyP1bOVhjzV2=vyu~LaCuRP;e66o5D+V;$S#rp33M>>W@HcQ>1`FCJapgU73 zQ8`GcdF)M$hv_sC7SpLDLOVK6d*31pFGluB0$j~;yTtgGv zMM%lpB~93|?9VrM+R_s`SA^zT3T4kfb=vW~1wL8*7NPm#tLt+M?$^)ER(E_G-|;gW z^dNZ4u=j*8jo7Vfk{~o`_a{lKZVY+19dxPKopl^*nH~wk}COF_cwJ%YBvUR zUIYNZzt9_npaOCL{s#Mh68Uei|F`}Z=Ks^>H@*Ke;WxeiEw^9m|H1Mo1TEcf6X35Z M|1~MU-Uk5w163c@eE 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; + } +} -- 2.7.4