summaryrefslogtreecommitdiffstats
path: root/auth/auth-service/src/main/java/org/onap/aaf/auth/service/api/API_Creds.java
blob: 964a05229a24f749079f111f2845c7cddbbdd3d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/**
 * ============LICENSE_START====================================================
 * org.onap.aaf
 * ===========================================================================
 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
 * ===========================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END====================================================
 *
 */

package org.onap.aaf.auth.service.api;

import static org.onap.aaf.auth.rserv.HttpMethods.DELETE;
import static org.onap.aaf.auth.rserv.HttpMethods.GET;
import static org.onap.aaf.auth.rserv.HttpMethods.POST;
import static org.onap.aaf.auth.rserv.HttpMethods.PUT;

import java.security.Principal;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.http.HttpStatus;
import org.onap.aaf.auth.direct.DirectAAFUserPass;
import org.onap.aaf.auth.env.AuthzTrans;
import org.onap.aaf.auth.layer.Result;
import org.onap.aaf.auth.rserv.HttpMethods;
import org.onap.aaf.auth.service.AAF_Service;
import org.onap.aaf.auth.service.Code;
import org.onap.aaf.auth.service.facade.AuthzFacade;
import org.onap.aaf.auth.service.mapper.Mapper.API;
import org.onap.aaf.cadi.CredVal;
import org.onap.aaf.cadi.CredVal.Type;
import org.onap.aaf.cadi.Symm;
import org.onap.aaf.cadi.principal.BasicPrincipal;
import org.onap.aaf.cadi.principal.X509Principal;
import org.onap.aaf.cadi.taf.basic.BasicHttpTaf;
import org.onap.aaf.misc.env.Env;
import org.onap.aaf.misc.env.TimeTaken;

/**
 * Initialize All Dispatches related to Credentials (AUTHN)
 * @author Jonathan
 *
 */
public class API_Creds {
    // Hide Public Interface
    private API_Creds() {}
    // needed to validate Creds even when already Authenticated x509
    /**
     * TIME SENSITIVE APIs
     * 
     * These will be first in the list
     * 
     * @param env
     * @param authzAPI
     * @param facade
     * @param directAAFUserPass 
     * @throws Exception
     */
    public static void timeSensitiveInit(Env env, AAF_Service authzAPI, AuthzFacade facade, final DirectAAFUserPass directAAFUserPass) throws Exception {
        /**
         * Basic Auth, quick Validation
         * 
         * Responds OK or NotAuthorized
         */
        authzAPI.route(env, HttpMethods.GET, "/authn/basicAuth", new Code(facade,"Is given BasicAuth valid?",true) {
            @Override
            public void handle(
                    AuthzTrans trans, 
                    HttpServletRequest req,
                    HttpServletResponse resp) throws Exception {

                Principal p = trans.getUserPrincipal();
                if (p instanceof BasicPrincipal) {
                    // the idea is that if call is made with this credential, and it's a BasicPrincipal, it's ok
                    // otherwise, it wouldn't have gotten here.
                    resp.setStatus(HttpStatus.OK_200);
                } else if (p instanceof X509Principal) {
                    // have to check Basic Auth here, because it might be CSP.
                    String authz = req.getHeader("Authorization");
                    if (authz.startsWith("Basic ")) {
                        BasicHttpTaf bht = ((X509Principal)p).getBasicHttpTaf(); 
                        if (bht!=null) {
                            BasicPrincipal bp = new BasicPrincipal(authz,"");
                            CredVal cv = bht.getCredVal(bp.getDomain());
                            if (cv!=null) {
                                if (cv.validate(bp.getName(), Type.PASSWORD, bp.getCred(), null) ) {
                                    resp.setStatus(HttpStatus.OK_200);
                                } else {
                                    resp.setStatus(HttpStatus.FORBIDDEN_403);
                                }
                            }
                        } else {
                            String decoded = Symm.base64noSplit.decode(authz.substring(6));
                            int colon = decoded.indexOf(':');
                            TimeTaken tt = trans.start("Direct Validation", Env.REMOTE);
                            try {
                                if (directAAFUserPass.validate(
                                        decoded.substring(0,colon), 
                                        CredVal.Type.PASSWORD , 
                                        decoded.substring(colon+1).getBytes(),trans)) {
                                    
                                    resp.setStatus(HttpStatus.OK_200);
                                } else {
                                    // DME2 at this version crashes without some sort of response
                                    resp.getOutputStream().print("");
                                    resp.setStatus(HttpStatus.FORBIDDEN_403);
                                }
                            } finally {
                                tt.done();
                            }
                        }
                    }
                } else if (p == null) {
                    trans.error().log("Transaction not Authenticated... no Principal");
                    resp.setStatus(HttpStatus.FORBIDDEN_403);
                } else {
                    trans.checkpoint("Basic Auth Check Failed: This wasn't a Basic Auth Trans");
                    // For Auth Security questions, we don't give any info to client on why failed
                    resp.setStatus(HttpStatus.FORBIDDEN_403);
                }
            }
        },"text/plain","*/*","*");
        
        /** 
         *  returns whether a given Credential is valid
         */
        authzAPI.route(POST, "/authn/validate", API.CRED_REQ, new Code(facade,"Is given Credential valid?",true) {
            @Override
            public void handle(
                    AuthzTrans trans, 
                    HttpServletRequest req,
                    HttpServletResponse resp) throws Exception {
                
                Result<Date> r = context.doesCredentialMatch(trans, req, resp);
                if (r.isOK()) {
                    resp.setStatus(HttpStatus.OK_200);
                } else {
                    // For Security, we don't give any info out on why failed, other than forbidden
                    // Can't do "401", because that is on the call itself
                    resp.setStatus(HttpStatus.FORBIDDEN_403);
                }
            }
        });  

        /** 
         *  returns whether a given Credential is valid
         */
        authzAPI.route(GET, "/authn/cert/id/:id", API.CERTS, new Code(facade,"Get Cert Info by ID",true) {
            @Override
            public void handle(
                    AuthzTrans trans, 
                    HttpServletRequest req,
                    HttpServletResponse resp) throws Exception {
                
                Result<Void> r = context.getCertInfoByID(trans, req, resp, pathParam(req,":id") );
                if (r.isOK()) {
                        resp.setStatus(HttpStatus.OK_200); 
                } else {
                        // For Security, we don't give any info out on why failed, other than forbidden
                        resp.setStatus(HttpStatus.FORBIDDEN_403);
                }
            }
        });  




    }
    
    /**
     * Normal Init level APIs
     * 
     * @param authzAPI
     * @param facade
     * @throws Exception
     */
    public static void init(AAF_Service authzAPI, AuthzFacade facade) throws Exception {
        /**
         * Create a new ID/Credential
         */
        authzAPI.route(POST,"/authn/cred",API.CRED_REQ,new Code(facade,"Add a New ID/Credential", true) {
            @Override
            public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {                
            	Result<Void> r = context.createUserCred(trans, req);
                if (r.isOK()) {
                    resp.setStatus(HttpStatus.CREATED_201);
                } else {
                    context.error(trans,resp,r);
                }
            }
        });
        
        /** 
         *  gets all credentials by Namespace
         */
        authzAPI.route(GET, "/authn/creds/ns/:ns", API.USERS, new Code(facade,"Get Creds for a Namespace",true) {
            @Override
            public void handle(
                    AuthzTrans trans, 
                    HttpServletRequest req,
                    HttpServletResponse resp) throws Exception {
                
                Result<Void> r = context.getCredsByNS(trans, resp, pathParam(req, "ns"));
                if (r.isOK()) {
                    resp.setStatus(HttpStatus.OK_200); 
                } else {
                    context.error(trans,resp,r);
                }
            }

        });
        
        /** 
         *  gets all credentials by ID
         */
        authzAPI.route(GET, "/authn/creds/id/:id", API.USERS, new Code(facade,"Get Creds by ID",true) {
            @Override
            public void handle(
                    AuthzTrans trans, 
                    HttpServletRequest req,
                    HttpServletResponse resp) throws Exception {
                
                Result<Void> r = context.getCredsByID(trans, resp, pathParam(req, "id"));
                if (r.isOK()) {
                    resp.setStatus(HttpStatus.OK_200); 
                } else {
                    context.error(trans,resp,r);
                }
            }

        });


        /**
         * Update ID/Credential (aka reset)
         */
        authzAPI.route(PUT,"/authn/cred",API.CRED_REQ,new Code(facade,"Update an ID/Credential", true) {
            @Override
            public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
                
                Result<Void> r = context.changeUserCred(trans, req);
                if (r.isOK()) {
                    resp.setStatus(HttpStatus.OK_200);
                } else {
                    context.error(trans,resp,r);
                }
            }
        });

        /**
         * Extend ID/Credential
         * This behavior will accelerate getting out of P1 outages due to ignoring renewal requests, or
         * other expiration issues.
         * 
         * Scenario is that people who are solving Password problems at night, are not necessarily those who
         * know what the passwords are supposed to be.  Also, changing Password, without changing Configurations
         * using that password only exacerbates the P1 Issue.
         */
        authzAPI.route(PUT,"/authn/cred/:days",API.CRED_REQ,new Code(facade,"Extend an ID/Credential", true) {
            @Override
            public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
                Result<Void> r = context.extendUserCred(trans, req, pathParam(req, "days"));
                if (r.isOK()) {
                    resp.setStatus(HttpStatus.OK_200);
                } else {
                    context.error(trans,resp,r);
                }
            }
        });

        /**
         * Delete a ID/Credential by Object
         */
        authzAPI.route(DELETE,"/authn/cred",API.CRED_REQ,new Code(facade,"Delete a Credential", true) {
            @Override
            public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
                Result<Void> r = context.deleteUserCred(trans, req);
                if (r.isOK()) {
                    resp.setStatus(HttpStatus.OK_200);
                } else {
                    context.error(trans,resp,r);
                }
            }
        });

    }
}