2366452d44fd43b1a3472496ef55cbdeb5d9878d
[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.util.DmaapConfig;
35
36
37         
38
39 public class AafLurAndFish implements ApiAuthorizationCheckInterface {
40         private AafLurService svc;
41         private static String api_namespace;
42         static final Logger logger = Logger.getLogger(AafLurAndFish.class);
43         
44         AafLurAndFish()  throws AuthenticationErrorException  {
45         
46                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
47                 api_namespace = p.getProperty( "ApiNamespace", "org.onap.dmaap-bc.api");
48
49                 String cadiprop = p.getProperty( "cadi.properties", "/opt/app/osaaf/local/org.onap.dmaap-bc.props");
50                 logger.info( "cadiprops in " + cadiprop );
51                 Properties props = new Properties();
52                 try {
53                         FileInputStream fis = new FileInputStream( cadiprop );
54                         try {
55                                 props.load( fis );
56                         } finally {
57                                 fis.close();
58                         }
59                 } catch ( IOException e ) {
60                         logger.error( "Unable to load " + cadiprop );
61                         logger.error("Error", e);
62                         throw new AuthenticationErrorException( );
63                 }
64                 try {
65                         PropAccess myAccess = new PropAccess( props );
66                 
67                         svc =  AafLurService.getInstance(myAccess);
68                 } catch (APIException | CadiException | LocatorException e ) {
69                         logger.error("Error", e);
70                         logger.error( e.toString() );
71                         throw new AuthenticationErrorException();
72                 }
73         
74         }
75         
76         public void check( String mechid, String pwd, DmaapPerm p ) throws AuthenticationErrorException {
77         
78                 try {
79                         boolean resp = svc.checkPerm( api_namespace, mechid, pwd, p );
80                         boolean flag = false;
81                         if ( resp == flag ) {
82                                 throw new AuthenticationErrorException();
83                         }
84                 } catch ( IOException | CadiException  e ) { 
85                         logger.error("Error", e);
86                         logger.error( e.toString() );
87                         throw new AuthenticationErrorException();
88                 }
89                 
90         }
91         
92            public static void main(String[] args) throws Exception {
93                 AafLurAndFish alaf = new AafLurAndFish();
94                 DmaapPerm p = new DmaapPerm( "org.onap.dmaap-bc.api.dmaap", "boot", "GET");
95                 
96                 alaf.check("demo@people.osaaf.org", "demo123456!", p);
97             }
98 }