5749a7aca8f576b914bd4d6c659c8b709022164a
[platform/upstream/iotivity.git] / cloud / account / src / main / java / org / iotivity / cloud / accountserver / AccountServerManager.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;
23
24 import java.util.ArrayList;
25 import java.util.Random;
26
27 import org.iotivity.cloud.accountserver.db.AccountDBManager;
28 import org.iotivity.cloud.accountserver.oauth.GitHub;
29 import org.iotivity.cloud.util.Logger;
30
31 /**
32  *
33  * This class provides a set of APIs to handle requests about account
34  * information of authorized user.
35  *
36  */
37 public class AccountServerManager {
38
39     /**
40      * API for requesting user account
41      *
42      * @param userId
43      *            user identifier
44      * @param deviceId
45      *            device identifier
46      * @return Boolean - true if registered, otherwise false
47      */
48     public Boolean registerUserAccount(String userId, String deviceId) {
49
50         Boolean ret = false;
51
52         // store info to OAuthDBManager
53         ret = AccountDBManager.getInstance().registerUserDevice(userId,
54                 deviceId);
55
56         return ret;
57     }
58
59     /**
60      * API for requesting user account and getting session code for registered
61      * user.
62      *
63      * @param userId
64      *            user identifier
65      * @return String - session code for registered user
66      */
67     public String registerUserAccount(String userId) {
68
69         String sessionCode = null;
70         sessionCode = generateSessionCode();
71
72         // store info to OAuthDBManager
73         AccountDBManager.getInstance().registerUserSessionCode(userId,
74                 sessionCode);
75
76         return sessionCode;
77     }
78
79     /**
80      * API for requesting user identifier corresponding with authorization
81      * information.
82      *
83      * @param authCode
84      *            authorization code
85      * @param authServer
86      *            authorization server
87      * @return String - user identifier
88      */
89     public String requestUserId(String authCode, String authServer) {
90
91         String userId = null;
92
93         String accessToken = getAccessToken(authCode, authServer);
94         userId = getUserId(accessToken, authServer);
95
96         return userId;
97     }
98
99     /**
100      * API for requesting user identifier corresponding with session code.
101      *
102      * @param sessionCode
103      *            session code
104      * @return String - user identifier
105      */
106     public String requestUserId(String sessionCode) {
107
108         String userId = null;
109
110         // get userId from MongDB
111         userId = AccountDBManager.getInstance().getUserId(sessionCode);
112
113         return userId;
114     }
115
116     /**
117      * API for getting devices corresponding with user identifier.
118      *
119      * @param userId
120      *            user identifier
121      * @return ArrayList<String> - list of devices
122      */
123     public ArrayList<String> requestAccountDevices(String userId) {
124
125         Logger.d("userId= " + userId);
126
127         ArrayList<String> deviceList = AccountDBManager.getInstance()
128                 .getDevices(userId);
129
130         return deviceList;
131     }
132
133     private String getAccessToken(String authCode, String authServer) {
134
135         String accessToken = null;
136
137         if (authServer.equals(Constants.GITHUB)) {
138
139             GitHub gitHub = new GitHub();
140             accessToken = gitHub.requestAccessToken(authCode);
141
142         } else {
143
144             Logger.e("unsupported auth.server = " + authServer);
145         }
146
147         return accessToken;
148     }
149
150     private String getUserId(String accessToken, String authServer) {
151
152         String userId = null;
153
154         if (authServer.equals(Constants.GITHUB)) {
155
156             GitHub gitHub = new GitHub();
157             userId = gitHub.requestGetUserInfo(accessToken);
158
159         } else {
160
161             Logger.e("unsupported auth.server = " + authServer);
162         }
163
164         return userId;
165     }
166
167     private String generateSessionCode() {
168
169         StringBuffer sessionCode = new StringBuffer();
170
171         Random random = new Random();
172         int randomNum = random.nextInt(122);
173         char code;
174
175         // generate 16byte key with 0-9, A-Z, a-z
176         for (int k = 0; k < 16; k++) {
177             while (true) {
178                 if ((randomNum >= 48 && randomNum <= 57)
179                         || (randomNum >= 65 && randomNum <= 90)
180                         || (randomNum >= 97 && randomNum <= 122)) {
181
182                     code = (char) randomNum;
183                     sessionCode.append(code);
184
185                     randomNum = random.nextInt(122);
186                     break;
187
188                 } else {
189
190                     randomNum = random.nextInt(122);
191                 }
192             }
193         }
194
195         return sessionCode.toString();
196     }
197 }