Removed unused class fields to prevent static
[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.auth.server.Log4JLogIt;
49 import org.onap.aaf.cadi.CadiException;
50 import org.onap.aaf.cadi.LocatorException;
51 import org.onap.aaf.cadi.PropAccess;
52 import org.onap.aaf.cadi.aaf.v2_0.AAFAuthn;
53 import org.onap.aaf.cadi.aaf.v2_0.AAFLurPerm;
54 import org.onap.aaf.cadi.aaf.v2_0.AAFTrustChecker;
55 import org.onap.aaf.cadi.aaf.v2_0.AbsAAFLocator;
56 import org.onap.aaf.cadi.config.Config;
57 import org.onap.aaf.cadi.oauth.TokenMgr;
58 import org.onap.aaf.cadi.oauth.TokenMgr.TokenPermLoader;
59 import org.onap.aaf.cadi.register.Registrant;
60 import org.onap.aaf.misc.env.APIException;
61 import org.onap.aaf.misc.env.Data.TYPE;
62 import org.onap.aaf.misc.env.Env;
63
64 import com.datastax.driver.core.Cluster;
65
66 import aafoauth.v2_0.Introspect;
67
68 public class AAF_OAuth extends AbsService<AuthzEnv,AuthzTrans> {
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      * @throws APIException
83      */
84     public AAF_OAuth(final AuthzEnv env) throws Exception {
85         super(env.access(),env);
86
87         String aaf_env = env.getProperty(Config.AAF_ENV);
88         if (aaf_env==null) {
89             throw new APIException("aaf_env needs to be set");
90         }
91
92         // Initialize Facade for all uses
93         AuthzTrans trans = env.newTrans();
94         cluster = org.onap.aaf.auth.dao.CassAccess.cluster(env,null);
95
96         aafLurPerm = aafCon().newLur();
97         // Note: If you need both Authn and Authz construct the following:
98         aafAuthn = aafCon().newAuthn(aafLurPerm);
99
100         // Start Background Processing
101         //    Question question =
102         question = new Question(trans, cluster, CassAccess.KEYSPACE);
103         question.startTimers(env);
104
105         // Have AAFLocator object Create DirectLocators for Location needs
106         AbsAAFLocator.setCreator(new DirectLocatorCreator(env, question.locateDAO));
107
108
109         service = new OAuthService(env.access(),trans,question);
110         facade1_0 = OAFacadeFactory.v1_0(this, trans, service, TYPE.JSON);
111         StringBuilder sb = new StringBuilder();
112         trans.auditTrail(2, sb);
113         trans.init().log(sb);
114
115         API_Token.init(this, facade1_0);
116     }
117
118     /**
119      * Setup XML and JSON implementations for each supported Version type
120      *
121      * We do this by taking the Code passed in and creating clones of these with the appropriate Facades and properties
122      * to do Versions and Content switches
123      *
124      */
125     public void route(HttpMethods meth, String path, API api, HttpCode<AuthzTrans, OAFacade<Introspect>> code) throws Exception {
126         String version = "1.0";
127         // Get Correct API Class from Mapper
128         Class<?> respCls = facade1_0.mapper().getClass(api);
129         if (respCls==null) throw new Exception("Unknown class associated with " + api.getClass().getName() + ' ' + api.name());
130         // setup Application API HTML ContentTypes for JSON and Route
131         String application = applicationJSON(respCls, version);
132         if (meth.equals(HttpMethods.POST)) {
133             route(env,meth,path,code,application,"application/json;version="+version,"application/x-www-form-urlencoded","*/*");
134         } else {
135             route(env,meth,path,code,application,"application/json;version="+version,"*/*");
136         }
137     }
138
139     @Override
140     public Filter[] _filters(Object ... additionalTafLurs) throws CadiException, LocatorException {
141         try {
142             DirectOAuthTAF doat = new DirectOAuthTAF(env,question,facade1_0);
143             Object[] atl=new Object[additionalTafLurs.length+2];
144             atl[0] = doat;
145             atl[1] = doat.directUserPass();
146
147             if (additionalTafLurs.length>0) {
148                 System.arraycopy(additionalTafLurs, 0, atl, 2, additionalTafLurs.length);
149             }
150
151             return new Filter[] {
152                 new AuthzTransFilter(env,aafCon(),
153                     new AAFTrustChecker((Env)env),
154                     atl
155             )};
156         } catch (NumberFormatException | APIException e) {
157             throw new CadiException("Invalid Property information", e);
158         }
159     }
160
161     @SuppressWarnings("unchecked")
162     @Override
163     public Registrant<AuthzEnv>[] registrants(final int port) throws CadiException {
164         return new Registrant[] {
165             new DirectRegistrar(access,question.locateDAO,port)
166         };
167     }
168
169
170     @Override
171     public void destroy() {
172         Cache.stopTimer();
173         if (service!=null) {
174             service.close();
175         }
176         if (cluster!=null) {
177             cluster.close();
178         }
179         super.destroy();
180     }
181
182     // For use in CADI ONLY
183     public TokenMgr.TokenPermLoader tpLoader() {
184         return tpLoader;
185     }
186
187     public static void main(final String[] args) {
188         try {
189             Log4JLogIt logIt = new Log4JLogIt(args, "oauth");
190             PropAccess propAccess = new PropAccess(logIt,args);
191
192             try {
193                 new JettyServiceStarter<AuthzEnv,AuthzTrans>(
194                     new AAF_OAuth(new AuthzEnv(propAccess)),true)
195                         .start();
196             } catch (Exception e) {
197                 propAccess.log(e);
198             }
199         } catch (Exception e) {
200             e.printStackTrace();
201         }
202     }
203 }