Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / cloud / account / src / main / java / org / iotivity / cloud / accountserver / oauth / GitHub.java
1 /*
2  * //******************************************************************
3  * //
4  * // Copyright 2016 Samsung Electronics All Rights Reserved.
5  * //
6  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7  * //
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
11  * //
12  * //      http://www.apache.org/licenses/LICENSE-2.0
13  * //
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.
19  * //
20  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21  */
22 package org.iotivity.cloud.accountserver.oauth;
23
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.util.JSONUtil;
36 import org.iotivity.cloud.util.Logger;
37
38 /**
39  *
40  * This class provides APIs relating authorization for GitHub.
41  *
42  */
43 public class GitHub extends OAuthServer {
44
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";
48
49     @Override
50     public String requestAccessToken(String authCode) {
51
52         String accessToken = null;
53
54         try {
55
56             OAuthClientRequest request = OAuthClientRequest
57                     .tokenProvider(OAuthProviderType.GITHUB)
58                     .setGrantType(GrantType.AUTHORIZATION_CODE)
59                     .setClientId(client_id).setClientSecret(secret)
60                     .setCode(authCode).buildBodyMessage();
61
62             OAuthClient oAuthClient = new OAuthClient(
63                     new URLConnectionClient());
64             GitHubTokenResponse oAuthResponse = oAuthClient.accessToken(request,
65                     GitHubTokenResponse.class);
66
67             accessToken = oAuthResponse.getAccessToken();
68
69         } catch (OAuthSystemException | OAuthProblemException e) {
70             e.printStackTrace();
71         }
72
73         return accessToken;
74     }
75
76     @Override
77     public String requestGetUserInfo(String accessToken) {
78
79         String userInfo = null;
80
81         if (accessToken == null) {
82             Logger.w("accessToken is null!");
83             return null;
84         }
85
86         try {
87
88             OAuthClientRequest request = new OAuthBearerClientRequest(
89                     resource_url).setAccessToken(accessToken)
90                     .buildQueryMessage();
91
92             OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
93             
94             OAuthResourceResponse resourceResponse = oAuthClient.resource(
95                     request, OAuth.HttpMethod.GET, OAuthResourceResponse.class);
96
97             userInfo = resourceResponse.getBody();
98             Logger.d("userInfo: " + userInfo);
99
100         } catch (OAuthSystemException | OAuthProblemException e) {
101             e.printStackTrace();
102         }
103
104         String userIdKey = "login";
105         String userId = JSONUtil.parseJSON(userInfo, userIdKey);
106
107         return userId;
108     }
109 }