Refactor Api Auth for AAF
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / authentication / AafLurAndFish.java
1 /*-
2  * ============LICENSE_START=======================================================
3   * org.onap.dmaap
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 package org.onap.dmaap.dbcapi.authentication;
21
22
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 import java.util.Properties;
26
27 import org.apache.log4j.Logger;
28 import org.onap.aaf.cadi.CadiException;
29 import org.onap.aaf.cadi.LocatorException;
30 import org.onap.aaf.cadi.PropAccess;
31 import org.onap.aaf.misc.env.APIException;
32 import org.onap.dmaap.dbcapi.aaf.AafLurService;
33 import org.onap.dmaap.dbcapi.aaf.DmaapPerm;
34 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
35 import org.onap.dmaap.dbcapi.server.Main;
36 import org.onap.dmaap.dbcapi.util.DmaapConfig;
37
38
39         
40
41 public class AafLurAndFish implements ApiAuthorizationCheckInterface {
42         private AafLurService svc;
43         private static String api_namespace;
44         static final Logger logger = Logger.getLogger(AafLurAndFish.class);
45         
46         AafLurAndFish()  throws AuthenticationErrorException  {
47         
48                 String[] args = new String[1];
49                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
50                 api_namespace = p.getProperty( "ApiNamespace", "org.onap.dmaap-bc.api");
51
52                 String cadiprop = p.getProperty( "cadi.properties", "/opt/app/osaaf/local/org.onap.dmaap-bc.props");
53                 logger.info( "cadiprops in " + cadiprop );
54                 Properties props = new Properties();
55                 try {
56                         FileInputStream fis = new FileInputStream( cadiprop );
57                         try {
58                                 props.load( fis );
59                         } finally {
60                                 fis.close();
61                         }
62                 } catch ( IOException e ) {
63                         logger.error( "Unable to load " + cadiprop );
64                         throw new AuthenticationErrorException( );
65                 }
66                 try {
67                         PropAccess myAccess = new PropAccess( props );
68                 
69                         svc =  AafLurService.getInstance(myAccess);
70                 } catch (APIException | CadiException | LocatorException e ) { 
71                         logger.error( e.toString() );
72                         throw new AuthenticationErrorException();
73                 }
74         
75         }
76         
77         public void check( String mechid, String pwd, DmaapPerm p ) throws AuthenticationErrorException {
78         
79                 try {
80                         boolean resp = svc.checkPerm( api_namespace, mechid, pwd, p );
81                         if ( resp == false ) {
82                                 throw new AuthenticationErrorException();
83                         }
84                 } catch ( IOException | CadiException  e ) { 
85                         logger.error( e.toString() );
86                         throw new AuthenticationErrorException();
87                 }
88                 
89         }
90         
91            public static void main(String[] args) throws Exception {
92                 AafLurAndFish alaf = new AafLurAndFish();
93                 DmaapPerm p = new DmaapPerm( "org.onap.dmaap-bc.api.dmaap", "boot", "GET");
94                 
95                 alaf.check("demo@people.osaaf.org", "demo123456!", p);
96             }
97 }