Fixed odd comment block formatting that was breaking tools.
[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.accountserver.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 = "{}";
80
81         try {
82
83             OAuthClientRequest request = new OAuthBearerClientRequest(
84                     resource_url).setAccessToken(accessToken)
85                             .buildQueryMessage();
86
87             OAuthClient oAuthClient = new OAuthClient(
88                     new URLConnectionClient());
89             OAuthResourceResponse resourceResponse = oAuthClient.resource(
90                     request, OAuth.HttpMethod.GET, OAuthResourceResponse.class);
91
92             userInfo = resourceResponse.getBody();
93             Logger.d("userInfo: " + userInfo);
94
95         } catch (OAuthSystemException | OAuthProblemException e) {
96             e.printStackTrace();
97         }
98
99         JSONUtil util = new JSONUtil();
100         String userIdKey = "login";
101         String userId = util.parseJSON(userInfo, userIdKey);
102
103         return userId;
104     }
105 }