Sonar Fixes, Formatting
[aaf/authz.git] / cadi / aaf / src / main / java / org / onap / aaf / cadi / oauth / OAuth2HttpTaf.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.cadi.oauth;
23
24 import java.security.NoSuchAlgorithmException;
25
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28
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;
42
43 public class OAuth2HttpTaf implements HttpTaf {
44     final private Access access;
45     final private TokenMgr tmgr;
46
47     public OAuth2HttpTaf(final Access access, final TokenMgr tmgr) {
48         this.tmgr = tmgr;
49         this.access = access;
50     }
51
52     @Override
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");
58             }
59             try {
60                 String tkn = authz.substring(7);
61                 Result<OAuth2Principal> rp = tmgr.toPrincipal(tkn,Hash.hashSHA256(tkn.getBytes()));
62                 if (rp.isOK()) {
63                     return new OAuth2HttpTafResp(access,rp.value,rp.value.getName()+" authenticated by Bearer Token",RESP.IS_AUTHENTICATED,resp,false);
64                 } else {
65                     return new OAuth2HttpTafResp(access,null,rp.error,RESP.FAIL,resp,true);
66                 }
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);
71             }
72         }
73         return new OAuth2HttpTafResp(access,null,"No OAuth2 ",RESP.TRY_ANOTHER_TAF,resp,true);
74     }
75
76     @Override
77     public Resp revalidate(CachedPrincipal prin,Object state) {
78         //TODO!!!!
79         return null;
80     }
81
82 }