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 API to handle requests for registering account
34 * information of authorized user, and publishing and finding resources.
37 public class AccountServerManager {
39 public Boolean registerUserAccount(String userId, String deviceId) {
43 // store info to OAuthDBManager
44 ret = AccountDBManager.getInstance().registerUserDevice(userId,
50 public String registerUserAccount(String userId) {
52 String sessionCode = null;
53 sessionCode = generateSessionCode();
55 // store info to OAuthDBManager
56 AccountDBManager.getInstance().registerUserSessionCode(userId,
63 * API for requesting user identifier to interested authorization server
68 * authorization server
69 * @return String - user identifier
71 public String requestUserId(String authCode, String authServer) {
75 String accessToken = getAccessToken(authCode, authServer);
76 userId = getUserId(accessToken, authServer);
81 public String requestUserId(String sessionCode) {
85 // get userId from MongDB
86 userId = AccountDBManager.getInstance().getUserId(sessionCode);
92 * API for getting devices according to authorized user from database
95 * identifier of authorized user
96 * @return ArrayList<String> - list of devices
98 public ArrayList<String> requestAccountDevices(String userId) {
100 Logger.d("userId= " + userId);
102 ArrayList<String> deviceList = AccountDBManager.getInstance()
109 * API for requesting access token to interested authorization server
112 * server name for authorization
115 * @return ArrayList<String> - array list of name of authorization servers
117 private String getAccessToken(String authCode, String authServer) {
119 String accessToken = null;
121 if (authServer.equals(Const.GITHUB)) {
123 GitHub gitHub = new GitHub();
124 accessToken = gitHub.requestAccessToken(authCode);
128 Logger.e("unsupported auth.server = " + authServer);
134 private String getUserId(String accessToken, String authServer) {
136 String userId = null;
138 if (authServer.equals(Const.GITHUB)) {
140 GitHub gitHub = new GitHub();
141 userId = gitHub.requestGetUserInfo(accessToken);
145 Logger.e("unsupported auth.server = " + authServer);
151 private String generateSessionCode() {
153 String sessionCode = "";
155 Random random = new Random();
156 int randomNum = random.nextInt(122);
159 // generate 16byte key with 0-9, A-Z, a-z
160 for (int k = 0; k < 16; k++) {
162 if ((randomNum >= 48 && randomNum <= 57)
163 || (randomNum >= 65 && randomNum <= 90)
164 || (randomNum >= 97 && randomNum <= 122)) {
166 code = (char) randomNum;
169 randomNum = random.nextInt(122);
174 randomNum = random.nextInt(122);