[Simulator] Minor UI changes fixing the IOT-1087.
[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 API to handle requests for registering account
34  * information of authorized user, and publishing and finding resources.
35  *
36  */
37 public class AccountServerManager {
38
39     public Boolean registerUserAccount(String userId, String deviceId) {
40
41         Boolean ret = false;
42
43         // store info to OAuthDBManager
44         ret = AccountDBManager.getInstance().registerUserDevice(userId,
45                 deviceId);
46
47         return ret;
48     }
49
50     public String registerUserAccount(String userId) {
51
52         String sessionCode = null;
53         sessionCode = generateSessionCode();
54
55         // store info to OAuthDBManager
56         AccountDBManager.getInstance().registerUserSessionCode(userId,
57                 sessionCode);
58
59         return sessionCode;
60     }
61
62     /**
63      * API for requesting user identifier to interested authorization server
64      * 
65      * @param accessToeken
66      *            access token
67      * @param authServer
68      *            authorization server
69      * @return String - user identifier
70      */
71     public String requestUserId(String authCode, String authServer) {
72
73         String userId = null;
74
75         String accessToken = getAccessToken(authCode, authServer);
76         userId = getUserId(accessToken, authServer);
77
78         return userId;
79     }
80
81     public String requestUserId(String sessionCode) {
82
83         String userId = null;
84
85         // get userId from MongDB
86         userId = AccountDBManager.getInstance().getUserId(sessionCode);
87
88         return userId;
89     }
90
91     /**
92      * API for getting devices according to authorized user from database
93      * 
94      * @param userId
95      *            identifier of authorized user
96      * @return ArrayList<String> - list of devices
97      */
98     public ArrayList<String> requestAccountDevices(String userId) {
99
100         Logger.d("userId= " + userId);
101
102         ArrayList<String> deviceList = AccountDBManager.getInstance()
103                 .getDevices(userId);
104
105         return deviceList;
106     }
107
108     /**
109      * API for requesting access token to interested authorization server
110      * 
111      * @param authServer
112      *            server name for authorization
113      * @param authCode
114      *            authorization code
115      * @return ArrayList<String> - array list of name of authorization servers
116      */
117     private String getAccessToken(String authCode, String authServer) {
118
119         String accessToken = null;
120
121         if (authServer.equals(Const.GITHUB)) {
122
123             GitHub gitHub = new GitHub();
124             accessToken = gitHub.requestAccessToken(authCode);
125
126         } else {
127
128             Logger.e("unsupported auth.server = " + authServer);
129         }
130
131         return accessToken;
132     }
133
134     private String getUserId(String accessToken, String authServer) {
135
136         String userId = null;
137
138         if (authServer.equals(Const.GITHUB)) {
139
140             GitHub gitHub = new GitHub();
141             userId = gitHub.requestGetUserInfo(accessToken);
142
143         } else {
144
145             Logger.e("unsupported auth.server = " + authServer);
146         }
147
148         return userId;
149     }
150
151     private String generateSessionCode() {
152
153         String sessionCode = "";
154
155         Random random = new Random();
156         int randomNum = random.nextInt(122);
157         char code;
158
159         // generate 16byte key with 0-9, A-Z, a-z
160         for (int k = 0; k < 16; k++) {
161             while (true) {
162                 if ((randomNum >= 48 && randomNum <= 57)
163                         || (randomNum >= 65 && randomNum <= 90)
164                         || (randomNum >= 97 && randomNum <= 122)) {
165
166                     code = (char) randomNum;
167                     sessionCode += code;
168
169                     randomNum = random.nextInt(122);
170                     break;
171
172                 } else {
173
174                     randomNum = random.nextInt(122);
175                 }
176             }
177         }
178
179         return sessionCode;
180     }
181 }