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;
24 import java.util.ArrayList;
25 import java.util.Random;
27 import org.iotivity.cloud.accountserver.db.AccountDBManager;
28 import org.iotivity.cloud.accountserver.oauth.GitHub;
29 import org.iotivity.cloud.util.Logger;
33 * This class provides a set of APIs to handle requests about account
34 * information of authorized user.
37 public class AccountServerManager {
40 * API for requesting user account
46 * @return Boolean - true if registered, otherwise false
48 public Boolean registerUserAccount(String userId, String deviceId) {
52 // store info to OAuthDBManager
53 ret = AccountDBManager.getInstance().registerUserDevice(userId,
60 * API for requesting user account and getting session code for registered
65 * @return String - session code for registered user
67 public String registerUserAccount(String userId) {
69 String sessionCode = null;
70 sessionCode = generateSessionCode();
72 // store info to OAuthDBManager
73 AccountDBManager.getInstance().registerUserSessionCode(userId,
80 * API for requesting user identifier corresponding with authorization
86 * authorization server
87 * @return String - user identifier
89 public String requestUserId(String authCode, String authServer) {
93 String accessToken = getAccessToken(authCode, authServer);
94 userId = getUserId(accessToken, authServer);
100 * API for requesting user identifier corresponding with session code.
104 * @return String - user identifier
106 public String requestUserId(String sessionCode) {
108 String userId = null;
110 // get userId from MongDB
111 userId = AccountDBManager.getInstance().getUserId(sessionCode);
117 * API for getting devices corresponding with user identifier.
121 * @return ArrayList<String> - list of devices
123 public ArrayList<String> requestAccountDevices(String userId) {
125 Logger.d("userId= " + userId);
127 ArrayList<String> deviceList = AccountDBManager.getInstance()
133 private String getAccessToken(String authCode, String authServer) {
135 String accessToken = null;
137 if (authServer.equals(Constants.GITHUB)) {
139 GitHub gitHub = new GitHub();
140 accessToken = gitHub.requestAccessToken(authCode);
144 Logger.e("unsupported auth.server = " + authServer);
150 private String getUserId(String accessToken, String authServer) {
152 String userId = null;
154 if (authServer.equals(Constants.GITHUB)) {
156 GitHub gitHub = new GitHub();
157 userId = gitHub.requestGetUserInfo(accessToken);
161 Logger.e("unsupported auth.server = " + authServer);
167 private String generateSessionCode() {
169 StringBuffer sessionCode = new StringBuffer();
171 Random random = new Random();
172 int randomNum = random.nextInt(122);
175 // generate 16byte key with 0-9, A-Z, a-z
176 for (int k = 0; k < 16; k++) {
178 if ((randomNum >= 48 && randomNum <= 57)
179 || (randomNum >= 65 && randomNum <= 90)
180 || (randomNum >= 97 && randomNum <= 122)) {
182 code = (char) randomNum;
183 sessionCode.append(code);
185 randomNum = random.nextInt(122);
190 randomNum = random.nextInt(122);
195 return sessionCode.toString();