2 *******************************************************************
4 * Copyright 2016 Samsung Electronics All Rights Reserved.
6 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 package org.iotivity.cloud.accountserver.oauth;
24 import org.apache.oltu.oauth2.client.OAuthClient;
25 import org.apache.oltu.oauth2.client.URLConnectionClient;
26 import org.apache.oltu.oauth2.client.request.OAuthBearerClientRequest;
27 import org.apache.oltu.oauth2.client.request.OAuthClientRequest;
28 import org.apache.oltu.oauth2.client.response.GitHubTokenResponse;
29 import org.apache.oltu.oauth2.client.response.OAuthResourceResponse;
30 import org.apache.oltu.oauth2.common.OAuth;
31 import org.apache.oltu.oauth2.common.OAuthProviderType;
32 import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
33 import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
34 import org.apache.oltu.oauth2.common.message.types.GrantType;
35 import org.iotivity.cloud.accountserver.util.JSONUtil;
36 import org.iotivity.cloud.util.Logger;
40 * This class provides APIs relating authorization for GitHub.
43 public class GitHub extends OAuthServer {
45 final static private String client_id = "ea9c18f540323b0213d0";
46 final static private String secret = "4bc0cd9fe21269507eb8eba3a32664a0f598dbc9";
47 final static private String resource_url = "https://api.github.com/user";
50 public String requestAccessToken(String authCode) {
52 String accessToken = null;
56 OAuthClientRequest request = OAuthClientRequest
57 .tokenProvider(OAuthProviderType.GITHUB)
58 .setGrantType(GrantType.AUTHORIZATION_CODE)
59 .setClientId(client_id).setClientSecret(secret)
60 .setCode(authCode).buildBodyMessage();
62 OAuthClient oAuthClient = new OAuthClient(
63 new URLConnectionClient());
64 GitHubTokenResponse oAuthResponse = oAuthClient.accessToken(request,
65 GitHubTokenResponse.class);
67 accessToken = oAuthResponse.getAccessToken();
69 } catch (OAuthSystemException | OAuthProblemException e) {
77 public String requestGetUserInfo(String accessToken) {
79 String userInfo = "{}";
83 OAuthClientRequest request = new OAuthBearerClientRequest(
84 resource_url).setAccessToken(accessToken)
87 OAuthClient oAuthClient = new OAuthClient(
88 new URLConnectionClient());
89 OAuthResourceResponse resourceResponse = oAuthClient.resource(
90 request, OAuth.HttpMethod.GET, OAuthResourceResponse.class);
92 userInfo = resourceResponse.getBody();
93 Logger.d("userInfo: " + userInfo);
95 } catch (OAuthSystemException | OAuthProblemException e) {
99 JSONUtil util = new JSONUtil();
100 String userIdKey = "login";
101 String userId = util.parseJSON(userInfo, userIdKey);