9defe34da128c03008a8cfed255691e925272dca
[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  * Modifications Copyright (C) 2019 IBM.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.dmaap.dbcapi.authentication;
22
23
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.util.Properties;
27
28 import org.apache.log4j.Logger;
29 import org.onap.aaf.cadi.CadiException;
30 import org.onap.aaf.cadi.LocatorException;
31 import org.onap.aaf.cadi.PropAccess;
32 import org.onap.aaf.misc.env.APIException;
33 import org.onap.dmaap.dbcapi.aaf.AafLurService;
34 import org.onap.dmaap.dbcapi.aaf.DmaapPerm;
35 import org.onap.dmaap.dbcapi.util.DmaapConfig;
36
37
38         
39
40 public class AafLurAndFish implements ApiAuthorizationCheckInterface {
41         private AafLurService svc;
42         private static String apiNamespace;
43         private static final String ERROR="Error";
44         static final Logger logger = Logger.getLogger(AafLurAndFish.class);
45         
46         AafLurAndFish()  throws AuthenticationErrorException  {
47         
48                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
49                 apiNamespace = p.getProperty( "ApiNamespace", "org.onap.dmaap-bc.api");
50
51                 String cadiprop = p.getProperty( "cadi.properties", "/opt/app/osaaf/local/org.onap.dmaap-bc.props");
52                 logger.info( "cadiprops in " + cadiprop );
53                 Properties props = new Properties();
54                 try {
55                         FileInputStream fis = new FileInputStream( cadiprop );
56                         try {
57                                 props.load( fis );
58                         } finally {
59                                 fis.close();
60                         }
61                 } catch ( IOException e ) {
62                         logger.error( "Unable to load " + cadiprop );
63                         logger.error(ERROR, e);
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(ERROR, e);
72                         logger.error( e.toString() );
73                         throw new AuthenticationErrorException();
74                 }
75         
76         }
77         
78         public void check( String mechid, String pwd, DmaapPerm p ) throws AuthenticationErrorException {
79         
80                 try {
81                         boolean resp = svc.checkPerm( apiNamespace, mechid, pwd, p );
82                         boolean flag = false;
83                         if ( resp == flag ) {
84                                 throw new AuthenticationErrorException();
85                         }
86                 } catch ( IOException | CadiException  e ) { 
87                         logger.error(ERROR, e);
88                         logger.error( e.toString() );
89                         throw new AuthenticationErrorException();
90                 }
91                 
92         }
93         
94          public static void main(String[] args) throws Exception {
95                 AafLurAndFish alaf = new AafLurAndFish();
96                 DmaapPerm p = new DmaapPerm( "org.onap.dmaap-bc.api.dmaap", "boot", "GET");
97                 
98                 try {
99                         alaf.check("mmanager@people.osaaf.org", "demo123456!", p);
100                 } catch (AuthenticationErrorException aee ) {
101                         logger.error(aee);
102                         logger.error( "Check failed for: " + p.toJSON());
103                         System.exit(-1);
104                 }
105                 logger.info("Check succeeded for: " + p.toJSON());
106             }
107 }