Sonar Fixes, Formatting
[aaf/authz.git] / auth / auth-oauth / src / main / java / org / onap / aaf / auth / oauth / mapper / MapperIntrospect1_0.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21
22 package org.onap.aaf.auth.oauth.mapper;
23
24 import java.util.Set;
25
26 import org.onap.aaf.auth.dao.cass.OAuthTokenDAO.Data;
27 import org.onap.aaf.auth.layer.Result;
28 import org.onap.aaf.auth.oauth.service.OAuthService.CLIENT_TYPE;
29
30 import aafoauth.v2_0.Introspect;
31
32 public class MapperIntrospect1_0 implements MapperIntrospect<Introspect> {
33
34     public Result<Introspect> introspect(Result<Data> rs) {
35         if (rs.isOKhasData()) {
36             Data data = rs.value;
37             Introspect ti = new Introspect();
38             ti.setAccessToken(data.id);
39             ti.setActive(data.active);
40             ti.setClientId(data.client_id);
41             for (CLIENT_TYPE ct : CLIENT_TYPE.values()) {
42                 if (data.type==ct.ordinal()) {
43                     ti.setClientType(ct.name());
44                     break;
45                 }
46             }
47             if (ti.getClientType()==null) {
48                 ti.setClientType(CLIENT_TYPE.unknown.name());
49             }
50             ti.setActive(data.active);
51             ti.setScope(getScopes(data.scopes(false)));
52             ti.setContent(data.content);
53             ti.setUsername(data.user);
54             ti.setExp(data.exp_sec); // want seconds from Jan 1, 1970
55             return Result.ok(ti);
56         }
57         return Result.err(rs);
58     }
59
60     protected static String getScopes(Set<String> scopes) {
61         StringBuilder sb = new StringBuilder();
62         boolean start = true;
63         for (String s : scopes) {
64             if (start) {
65                 start = false;
66             } else {
67                 sb.append(' ');
68             }
69             sb.append(s);
70         }
71         return sb.toString();
72     }
73
74 }