2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.auth.oauth.mapper;
25 import java.util.Map.Entry;
28 import javax.servlet.http.HttpServletRequest;
30 import org.onap.aaf.auth.dao.cass.OAuthTokenDAO;
31 import org.onap.aaf.auth.dao.cass.OAuthTokenDAO.Data;
32 import org.onap.aaf.auth.layer.Result;
33 import org.onap.aaf.auth.oauth.service.OAuthService;
34 import org.onap.aaf.auth.oauth.service.OCreds;
35 import org.onap.aaf.auth.oauth.service.OAuthService.CLIENT_TYPE;
36 import org.onap.aaf.auth.oauth.service.OAuthService.GRANT_TYPE;
37 import org.onap.aaf.cadi.client.Holder;
38 import org.onap.aaf.cadi.oauth.OAuth2Principal;
39 import org.onap.aaf.cadi.util.Vars;
40 import org.onap.aaf.misc.env.util.Split;
42 import aaf.v2_0.Error;
43 import aafoauth.v2_0.Introspect;
44 import aafoauth.v2_0.Token;
45 import aafoauth.v2_0.TokenRequest;
48 public class Mapper1_0 extends MapperIntrospect1_0 implements Mapper<TokenRequest,Token,Introspect,Error> {
50 public Class<?> getClass(API api) {
52 case TOKEN_REQ: return TokenRequest.class;
53 case TOKEN: return Token.class;
54 case INTROSPECT: return Introspect.class;
55 case ERROR: return Error.class;
56 case VOID: return Void.class;
61 @SuppressWarnings("unchecked")
63 public <A> A newInstance(API api) {
65 case TOKEN_REQ: return (A)new TokenRequest();
66 case TOKEN: return (A)new Token();
67 case INTROSPECT: return (A)new Introspect();
68 case ERROR: return (A)new Error();
69 case VOID: return null;
74 ////////////// Mapping Functions /////////////
76 public Error errorFromMessage(StringBuilder holder, String msgID, String text, String... var) {
77 Error err = new Error();
78 err.setMessageId(msgID);
79 // AT&T Restful Error Format requires numbers "%" placements
80 err.setText(Vars.convert(holder, text, var));
82 err.getVariables().add(s);
88 public TokenRequest tokenReqFromParams(HttpServletRequest req) {
89 TokenRequest tr = new TokenRequest();
91 @SuppressWarnings("unchecked")
92 Map<String, String[]> map = req.getParameterMap();
93 for(Entry<String, String[]> es : map.entrySet()) {
96 if(es.getValue().length==1) {
97 tr.setClientId(es.getValue()[0]);
101 case "client_secret":
102 if(es.getValue().length==1) {
103 tr.setClientSecret(es.getValue()[0]);
108 if(es.getValue().length==1) {
109 tr.setUsername(es.getValue()[0]);
114 if(es.getValue().length==1) {
115 tr.setPassword(es.getValue()[0]);
120 if(es.getValue().length==1) {
121 tr.setScope(es.getValue()[0]);
126 if(es.getValue().length==1) {
127 tr.setGrantType(es.getValue()[0]);
131 case "refresh_token":
132 if(es.getValue().length==1) {
133 tr.setRefreshToken(es.getValue()[0]);
146 * @see org.onap.aaf.auth.oauth.mapper.Mapper#credsFromReq(javax.servlet.http.HttpServletRequest)
149 public OCreds credsFromReq(TokenRequest tokReq) {
150 return new OCreds(tokReq.getClientId(),tokReq.getClientSecret(),
151 tokReq.getUsername(),tokReq.getPassword());
155 * @see org.onap.aaf.auth.oauth.mapper.Mapper#tokenReq(java.lang.Object)
158 public Data clientTokenReq(TokenRequest tokReq, Holder<GRANT_TYPE> hgt) {
159 OAuthTokenDAO.Data tdd = new OAuthTokenDAO.Data();
160 tdd.client_id = tokReq.getClientId();
161 tdd.user = tokReq.getUsername();
162 if(tokReq.getRefreshToken()!=null) {
163 tdd.refresh=tokReq.getRefreshToken();
166 for(GRANT_TYPE ttt : GRANT_TYPE.values()) {
167 if(ttt.name().equals(tokReq.getGrantType())) {
174 case client_credentials:
177 tdd.type = CLIENT_TYPE.confidential.ordinal();
180 tdd.type = CLIENT_TYPE.unknown.ordinal();
183 String scopes=tokReq.getScope();
185 Set<String> ss = tdd.scopes(true);
186 for(String s: Split.split(' ', tokReq.getScope())) {
191 tdd.state = tokReq.getState();
196 public Result<Token> tokenFromData(Result<Data> rd) {
198 return Result.err(rd);
201 Token token = new Token();
202 if(OAuthService.TOKEN_TYPE.values().length>d.type) {
203 token.setTokenType(OAuthService.TOKEN_TYPE.values()[d.type].name());
205 token.setTokenType("Invalid");
207 token.setAccessToken(d.id);
208 token.setRefreshToken(d.refresh);
209 token.setExpiresIn((int)(d.exp_sec-(System.currentTimeMillis())/1000));
210 token.setScope(getScopes(d.scopes(false)));
211 token.setState(d.state);
212 return Result.ok(token);
218 * @see org.onap.aaf.auth.oauth.mapper.Mapper#fromPrincipal(org.onap.aaf.cadi.oauth.OAuth2Principal)
221 public Introspect fromPrincipal(OAuth2Principal p) {
222 return p.tokenPerm().getIntrospect();