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.locate.api;
24 import java.net.ConnectException;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
29 import org.eclipse.jetty.http.HttpStatus;
30 import org.onap.aaf.auth.dao.hl.Question;
31 import org.onap.aaf.auth.env.AuthzTrans;
32 import org.onap.aaf.auth.locate.AAF_Locate;
33 import org.onap.aaf.auth.locate.BasicAuthCode;
34 import org.onap.aaf.auth.locate.LocateCode;
35 import org.onap.aaf.auth.locate.facade.LocateFacade;
36 import org.onap.aaf.auth.locate.mapper.Mapper.API;
37 import org.onap.aaf.auth.rserv.HttpMethods;
38 import org.onap.aaf.cadi.CadiException;
39 import org.onap.aaf.cadi.Symm;
40 import org.onap.aaf.cadi.client.Future;
41 import org.onap.aaf.cadi.client.Rcli;
42 import org.onap.aaf.cadi.client.Retryable;
43 import org.onap.aaf.cadi.config.Config;
44 import org.onap.aaf.cadi.oauth.OAuth2Principal;
45 import org.onap.aaf.misc.env.APIException;
46 import org.onap.aaf.misc.env.Env;
47 import org.onap.aaf.misc.env.LogTarget;
48 import org.onap.aaf.misc.env.TimeTaken;
51 * API Apis.. using Redirect for mechanism
56 public class API_Proxy {
59 * Normal Init level APIs
65 public static void init(final AAF_Locate gwAPI, LocateFacade facade, final Question question) {
67 String aafurl = gwAPI.access.getProperty(Config.AAF_URL,null);
71 // But DO NOT transfer BasicAuth case... wastes resources.
73 final BasicAuthCode bac = new BasicAuthCode(gwAPI.aafAuthn,facade);
75 gwAPI.routeAll(HttpMethods.GET,"/proxy/:path*",API.VOID,new LocateCode(facade,"Proxy GET", true) {
77 public void handle(final AuthzTrans trans, final HttpServletRequest req, final HttpServletResponse resp) throws Exception {
78 populateCredentialTag(trans, req, question);
79 if ("/proxy/authn/basicAuth".equals(req.getPathInfo()) && !(req.getUserPrincipal() instanceof OAuth2Principal)) {
80 bac.handle(trans, req, resp);
82 TimeTaken tt = trans.start("Forward to AAF Service", Env.REMOTE);
84 gwAPI.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
86 public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
87 Future<Void> ft = client.transfer(req,resp,pathParam(req, ":path"),HttpStatus.OK_200);
88 ft.get(10000); // Covers return codes and err messages
93 } catch (CadiException | APIException e) {
102 gwAPI.routeAll(HttpMethods.POST,"/proxy/:path*",API.VOID,new LocateCode(facade,"Proxy POST", true) {
104 public void handle(final AuthzTrans trans, final HttpServletRequest req, final HttpServletResponse resp) throws Exception {
105 TimeTaken tt = trans.start("Forward to AAF Service", Env.REMOTE);
107 gwAPI.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
109 public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
110 Future<Void> ft = client.transfer(req,resp,pathParam(req, ":path"),HttpStatus.CREATED_201);
111 ft.get(10000); // Covers return codes and err messages
115 } catch (CadiException | APIException e) {
116 trans.error().log(e);
123 gwAPI.routeAll(HttpMethods.PUT,"/proxy/:path*",API.VOID,new LocateCode(facade,"Proxy PUT", true) {
125 public void handle(final AuthzTrans trans, final HttpServletRequest req, final HttpServletResponse resp) throws Exception {
126 TimeTaken tt = trans.start("Forward to AAF Service", Env.REMOTE);
128 gwAPI.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
130 public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
131 Future<Void> ft = client.transfer(req,resp,pathParam(req, ":path"),HttpStatus.OK_200);
132 ft.get(10000); // Covers return codes and err messages
136 } catch (CadiException | APIException e) {
137 trans.error().log(e);
144 gwAPI.routeAll(HttpMethods.DELETE,"/proxy/:path*",API.VOID,new LocateCode(facade,"Proxy DELETE", true) {
146 public void handle(final AuthzTrans trans, final HttpServletRequest req, final HttpServletResponse resp) throws Exception {
147 TimeTaken tt = trans.start("Forward to AAF Service", Env.REMOTE);
149 gwAPI.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
151 public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
152 Future<Void> ft = client.transfer(req,resp,pathParam(req, ":path"),HttpStatus.OK_200);
153 ft.get(10000); // Covers return codes and err messages
157 } catch (CadiException | APIException e) {
158 trans.error().log(e);
168 * Populates TAG value for the user from DB
174 private static void populateCredentialTag(AuthzTrans trans, HttpServletRequest req, Question question) {
177 String authz = req.getHeader("Authorization");
178 String decoded = Symm.base64noSplit.decode(authz.substring(6));
179 int colon = decoded.indexOf(':');
180 // Update transaction object with TAG information from DB
181 question.doesUserCredMatch(trans, decoded.substring(0, colon), decoded.substring(colon + 1).getBytes());
182 String tag = trans.getTag();
184 req.setAttribute("CRED_TAG", tag);
186 } catch (Exception e) {
187 LogTarget lt = trans.error();
188 lt.log("Exception occured while fetching TAG details from DB :" + e.getMessage());