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.cadi.oauth;
24 import java.security.NoSuchAlgorithmException;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
29 import org.onap.aaf.cadi.Access;
30 import org.onap.aaf.cadi.CachedPrincipal;
31 import org.onap.aaf.cadi.CadiException;
32 import org.onap.aaf.cadi.Hash;
33 import org.onap.aaf.cadi.LocatorException;
34 import org.onap.aaf.cadi.Access.Level;
35 import org.onap.aaf.cadi.CachedPrincipal.Resp;
36 import org.onap.aaf.cadi.Taf.LifeForm;
37 import org.onap.aaf.cadi.client.Result;
38 import org.onap.aaf.cadi.taf.HttpTaf;
39 import org.onap.aaf.cadi.taf.TafResp;
40 import org.onap.aaf.cadi.taf.TafResp.RESP;
41 import org.onap.aaf.misc.env.APIException;
43 public class OAuth2HttpTaf implements HttpTaf {
44 final private Access access;
45 final private TokenMgr tmgr;
47 public OAuth2HttpTaf(final Access access, final TokenMgr tmgr) {
53 public TafResp validate(LifeForm reading, HttpServletRequest req, HttpServletResponse resp) {
54 String authz = req.getHeader("Authorization");
55 if (authz != null && authz.length()>7 && authz.startsWith("Bearer ")) {
56 if (!req.isSecure()) {
57 access.log(Level.WARN,"WARNING! OAuth has been used over an insecure channel");
60 String tkn = authz.substring(7);
61 Result<OAuth2Principal> rp = tmgr.toPrincipal(tkn,Hash.hashSHA256(tkn.getBytes()));
63 return new OAuth2HttpTafResp(access,rp.value,rp.value.getName()+" authenticated by Bearer Token",RESP.IS_AUTHENTICATED,resp,false);
65 return new OAuth2HttpTafResp(access,null,rp.error,RESP.FAIL,resp,true);
67 } catch (APIException | CadiException | LocatorException e) {
68 return new OAuth2HttpTafResp(access,null,"Bearer Token invalid",RESP.FAIL,resp,true);
69 } catch (NoSuchAlgorithmException e) {
70 return new OAuth2HttpTafResp(access,null,"Security Algorithm not available",RESP.FAIL,resp,true);
73 return new OAuth2HttpTafResp(access,null,"No OAuth2 ",RESP.TRY_ANOTHER_TAF,resp,true);
77 public Resp revalidate(CachedPrincipal prin,Object state) {