AT&T 2.0.19 Code drop, stage 3
[aaf/authz.git] / auth / auth-oauth / src / main / java / org / onap / aaf / auth / oauth / AAF_OAuth.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
23 package org.onap.aaf.auth.oauth;
24
25 import java.util.Map;
26
27 import javax.servlet.Filter;
28
29 import org.onap.aaf.auth.cache.Cache;
30 import org.onap.aaf.auth.cache.Cache.Dated;
31 import org.onap.aaf.auth.dao.CassAccess;
32 import org.onap.aaf.auth.dao.hl.Question;
33 import org.onap.aaf.auth.direct.DirectLocatorCreator;
34 import org.onap.aaf.auth.direct.DirectRegistrar;
35 import org.onap.aaf.auth.env.AuthzEnv;
36 import org.onap.aaf.auth.env.AuthzTrans;
37 import org.onap.aaf.auth.env.AuthzTransFilter;
38 import org.onap.aaf.auth.oauth.api.API_Token;
39 import org.onap.aaf.auth.oauth.facade.OAFacade;
40 import org.onap.aaf.auth.oauth.facade.OAFacade1_0;
41 import org.onap.aaf.auth.oauth.facade.OAFacadeFactory;
42 import org.onap.aaf.auth.oauth.mapper.Mapper.API;
43 import org.onap.aaf.auth.oauth.service.OAuthService;
44 import org.onap.aaf.auth.rserv.HttpCode;
45 import org.onap.aaf.auth.rserv.HttpMethods;
46 import org.onap.aaf.auth.server.AbsService;
47 import org.onap.aaf.auth.server.JettyServiceStarter;
48 import org.onap.aaf.cadi.CadiException;
49 import org.onap.aaf.cadi.LocatorException;
50 import org.onap.aaf.cadi.PropAccess;
51 import org.onap.aaf.cadi.aaf.v2_0.AAFAuthn;
52 import org.onap.aaf.cadi.aaf.v2_0.AAFLurPerm;
53 import org.onap.aaf.cadi.aaf.v2_0.AAFTrustChecker;
54 import org.onap.aaf.cadi.aaf.v2_0.AbsAAFLocator;
55 import org.onap.aaf.cadi.config.Config;
56 import org.onap.aaf.cadi.oauth.TokenMgr;
57 import org.onap.aaf.cadi.oauth.TokenMgr.TokenPermLoader;
58 import org.onap.aaf.cadi.register.Registrant;
59 import org.onap.aaf.misc.env.APIException;
60 import org.onap.aaf.misc.env.Env;
61 import org.onap.aaf.misc.env.Data.TYPE;
62
63 import com.datastax.driver.core.Cluster;
64
65 import aafoauth.v2_0.Introspect;
66
67 public class AAF_OAuth extends AbsService<AuthzEnv,AuthzTrans> {
68         private static final String DOT_OAUTH = ".oauth";
69         public Map<String, Dated> cacheUser;
70         public AAFAuthn<?> aafAuthn;
71         public AAFLurPerm aafLurPerm;
72         private final OAuthService service;
73         private OAFacade1_0 facade1_0;
74         private final Question question;
75         private TokenPermLoader tpLoader; 
76         private final Cluster cluster;
77         
78         /**
79          * Construct AuthzAPI with all the Context Supporting Routes that Authz needs
80          * 
81          * @param env
82          * @param si 
83          * @param dm 
84          * @param decryptor 
85          * @throws APIException 
86          */
87         public AAF_OAuth(final AuthzEnv env) throws Exception {
88                 super(env.access(),env);
89                 
90                 String aaf_env = env.getProperty(Config.AAF_ENV);
91                 if(aaf_env==null) {
92                         throw new APIException("aaf_env needs to be set");
93                 }
94                 
95                 // Initialize Facade for all uses
96                 AuthzTrans trans = env.newTrans();
97                 cluster = org.onap.aaf.auth.dao.CassAccess.cluster(env,null);
98                 
99                 aafLurPerm = aafCon().newLur();
100                 // Note: If you need both Authn and Authz construct the following:
101                 aafAuthn = aafCon().newAuthn(aafLurPerm);
102
103                 // Start Background Processing
104                 //      Question question = 
105                 question = new Question(trans, cluster, CassAccess.KEYSPACE, true);
106
107                 // Have AAFLocator object Create DirectLocators for Location needs
108                 AbsAAFLocator.setCreator(new DirectLocatorCreator(env, question.locateDAO));
109
110
111                 service = new OAuthService(env.access(),trans,question);
112                 facade1_0 = OAFacadeFactory.v1_0(this, trans, service, TYPE.JSON);
113                 StringBuilder sb = new StringBuilder();
114                 trans.auditTrail(2, sb);
115                 trans.init().log(sb);
116                 
117                 API_Token.init(this, facade1_0);
118         }
119         
120         /**
121          * Setup XML and JSON implementations for each supported Version type
122          * 
123          * We do this by taking the Code passed in and creating clones of these with the appropriate Facades and properties
124          * to do Versions and Content switches
125          * 
126          */
127         public void route(HttpMethods meth, String path, API api, HttpCode<AuthzTrans, OAFacade<Introspect>> code) throws Exception {
128                 String version = "1.0";
129                 // Get Correct API Class from Mapper
130                 Class<?> respCls = facade1_0.mapper().getClass(api); 
131                 if(respCls==null) throw new Exception("Unknown class associated with " + api.getClass().getName() + ' ' + api.name());
132                 // setup Application API HTML ContentTypes for JSON and Route
133                 String application = applicationJSON(respCls, version);
134                 if(meth.equals(HttpMethods.POST)) {
135                         route(env,meth,path,code,application,"application/json;version="+version,"application/x-www-form-urlencoded","*/*");
136                 } else {
137                         route(env,meth,path,code,application,"application/json;version="+version,"*/*");
138                 }
139         }
140         
141         @Override
142         public Filter[] filters() throws CadiException, LocatorException {
143                 try {
144                 DirectOAuthTAF doat;
145                         return new Filter[] {new AuthzTransFilter(env,aafCon(),
146                                 new AAFTrustChecker((Env)env),
147                                 doat = new DirectOAuthTAF(env,question,facade1_0),
148                                 doat.directUserPass()
149                                 )};
150                 } catch (NumberFormatException | APIException e) {
151                         throw new CadiException("Invalid Property information", e);
152                 }
153         }
154
155         
156         @SuppressWarnings("unchecked")
157         @Override
158         public Registrant<AuthzEnv>[] registrants(final int port) throws CadiException {
159                 return new Registrant[] {
160                                 new DirectRegistrar(access,question.locateDAO,app_name,app_version,port),
161                                 new DirectRegistrar(access,question.locateDAO,app_name.replace(DOT_OAUTH, ".token"),app_version,port),
162                                 new DirectRegistrar(access,question.locateDAO,app_name.replace(DOT_OAUTH, ".introspect"),app_version,port)
163
164                 };
165         }
166
167
168         @Override
169         public void destroy() {
170                 Cache.stopTimer();
171                 if(service!=null) {
172                         service.close();
173                 }
174                 if(cluster!=null) {
175                         cluster.close();
176                 }
177                 super.destroy();
178         }
179         
180         // For use in CADI ONLY
181         public TokenMgr.TokenPermLoader tpLoader() {
182                 return tpLoader;
183         }
184
185         public static void main(final String[] args) {
186                 PropAccess propAccess = new PropAccess(args);
187                 try {
188                         AAF_OAuth service = new AAF_OAuth(new AuthzEnv(propAccess));
189 //                      env.setLog4JNames("log4j.properties","authz","oauth","audit","init","trace");
190                         JettyServiceStarter<AuthzEnv,AuthzTrans> jss = new JettyServiceStarter<AuthzEnv,AuthzTrans>(service);
191                         jss.start();
192                 } catch (Exception e) {
193                         e.printStackTrace();
194                 }
195         }
196 }