1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * ===========================================================================
\r
7 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * * you may not use this file except in compliance with the License.
\r
9 * * You may obtain a copy of the License at
\r
11 * * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * * Unless required by applicable law or agreed to in writing, software
\r
14 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * * See the License for the specific language governing permissions and
\r
17 * * limitations under the License.
\r
18 * * ============LICENSE_END====================================================
\r
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
23 package com.att.authz.service.api;
\r
25 import static com.att.cssa.rserv.HttpMethods.DELETE;
\r
26 import static com.att.cssa.rserv.HttpMethods.GET;
\r
27 import static com.att.cssa.rserv.HttpMethods.POST;
\r
28 import static com.att.cssa.rserv.HttpMethods.PUT;
\r
30 import java.security.Principal;
\r
31 import java.util.Date;
\r
33 import javax.servlet.http.HttpServletRequest;
\r
34 import javax.servlet.http.HttpServletResponse;
\r
36 import com.att.aft.dme2.internal.jetty.http.HttpStatus;
\r
37 import com.att.authz.cadi.DirectAAFUserPass;
\r
38 import com.att.authz.env.AuthzTrans;
\r
39 import com.att.authz.facade.AuthzFacade;
\r
40 import com.att.authz.layer.Result;
\r
41 import com.att.authz.service.AuthAPI;
\r
42 import com.att.authz.service.Code;
\r
43 import com.att.authz.service.mapper.Mapper.API;
\r
44 import com.att.cadi.CredVal;
\r
45 import com.att.cadi.Symm;
\r
46 import com.att.cadi.principal.BasicPrincipal;
\r
47 import com.att.cadi.principal.X509Principal;
\r
48 import com.att.cssa.rserv.HttpMethods;
\r
49 import com.att.inno.env.Env;
\r
52 * Initialize All Dispatches related to Credentials (AUTHN)
\r
55 public class API_Creds {
\r
56 // Hide Public Interface
\r
57 private API_Creds() {}
\r
58 // needed to validate Creds even when already Authenticated x509
\r
60 * TIME SENSITIVE APIs
\r
62 * These will be first in the list
\r
67 * @param directAAFUserPass
\r
70 public static void timeSensitiveInit(Env env, AuthAPI authzAPI, AuthzFacade facade, final DirectAAFUserPass directAAFUserPass) throws Exception {
\r
72 * Basic Auth, quick Validation
\r
74 * Responds OK or NotAuthorized
\r
76 authzAPI.route(env, HttpMethods.GET, "/authn/basicAuth", new Code(facade,"Is given BasicAuth valid?",true) {
\r
80 HttpServletRequest req,
\r
81 HttpServletResponse resp) throws Exception {
\r
83 Principal p = trans.getUserPrincipal();
\r
84 if (p instanceof BasicPrincipal) {
\r
85 // the idea is that if call is made with this credential, and it's a BasicPrincipal, it's ok
\r
86 // otherwise, it wouldn't have gotten here.
\r
87 resp.setStatus(HttpStatus.OK_200);
\r
88 } else if (p instanceof X509Principal) {
\r
89 // have to check Basic Auth here, because it might be CSP.
\r
90 String ba = req.getHeader("Authorization");
\r
91 if(ba.startsWith("Basic ")) {
\r
92 String decoded = Symm.base64noSplit.decode(ba.substring(6));
\r
93 int colon = decoded.indexOf(':');
\r
94 if(directAAFUserPass.validate(
\r
95 decoded.substring(0,colon),
\r
96 CredVal.Type.PASSWORD ,
\r
97 decoded.substring(colon+1).getBytes())) {
\r
99 resp.setStatus(HttpStatus.OK_200);
\r
101 resp.setStatus(HttpStatus.FORBIDDEN_403);
\r
104 } else if(p == null) {
\r
105 trans.error().log("Transaction not Authenticated... no Principal");
\r
106 resp.setStatus(HttpStatus.FORBIDDEN_403);
\r
108 trans.checkpoint("Basic Auth Check Failed: This wasn't a Basic Auth Trans");
\r
109 // For Auth Security questions, we don't give any info to client on why failed
\r
110 resp.setStatus(HttpStatus.FORBIDDEN_403);
\r
116 * returns whether a given Credential is valid
\r
118 authzAPI.route(POST, "/authn/validate", API.CRED_REQ, new Code(facade,"Is given Credential valid?",true) {
\r
120 public void handle(
\r
122 HttpServletRequest req,
\r
123 HttpServletResponse resp) throws Exception {
\r
125 Result<Date> r = context.doesCredentialMatch(trans, req, resp);
\r
127 resp.setStatus(HttpStatus.OK_200);
\r
129 // For Security, we don't give any info out on why failed, other than forbidden
\r
130 resp.setStatus(HttpStatus.FORBIDDEN_403);
\r
136 * returns whether a given Credential is valid
\r
138 authzAPI.route(GET, "/authn/cert/id/:id", API.CERTS, new Code(facade,"Get Cert Info by ID",true) {
\r
140 public void handle(
\r
142 HttpServletRequest req,
\r
143 HttpServletResponse resp) throws Exception {
\r
145 Result<Void> r = context.getCertInfoByID(trans, req, resp, pathParam(req,":id") );
\r
147 resp.setStatus(HttpStatus.OK_200);
\r
149 // For Security, we don't give any info out on why failed, other than forbidden
\r
150 resp.setStatus(HttpStatus.FORBIDDEN_403);
\r
161 * Normal Init level APIs
\r
165 * @throws Exception
\r
167 public static void init(AuthAPI authzAPI, AuthzFacade facade) throws Exception {
\r
169 * Create a new ID/Credential
\r
171 authzAPI.route(POST,"/authn/cred",API.CRED_REQ,new Code(facade,"Add a New ID/Credential", true) {
\r
173 public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
\r
174 Result<Void> r = context.createUserCred(trans, req);
\r
176 resp.setStatus(HttpStatus.CREATED_201);
\r
178 context.error(trans,resp,r);
\r
184 * gets all credentials by Namespace
\r
186 authzAPI.route(GET, "/authn/creds/ns/:ns", API.USERS, new Code(facade,"Get Creds for a Namespace",true) {
\r
188 public void handle(
\r
190 HttpServletRequest req,
\r
191 HttpServletResponse resp) throws Exception {
\r
193 Result<Void> r = context.getCredsByNS(trans, resp, pathParam(req, "ns"));
\r
195 resp.setStatus(HttpStatus.OK_200);
\r
197 context.error(trans,resp,r);
\r
204 * gets all credentials by ID
\r
206 authzAPI.route(GET, "/authn/creds/id/:id", API.USERS, new Code(facade,"Get Creds by ID",true) {
\r
208 public void handle(
\r
210 HttpServletRequest req,
\r
211 HttpServletResponse resp) throws Exception {
\r
213 Result<Void> r = context.getCredsByID(trans, resp, pathParam(req, "id"));
\r
215 resp.setStatus(HttpStatus.OK_200);
\r
217 context.error(trans,resp,r);
\r
225 * Update ID/Credential (aka reset)
\r
227 authzAPI.route(PUT,"/authn/cred",API.CRED_REQ,new Code(facade,"Update an ID/Credential", true) {
\r
229 public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
\r
231 Result<Void> r = context.changeUserCred(trans, req);
\r
233 resp.setStatus(HttpStatus.OK_200);
\r
235 context.error(trans,resp,r);
\r
241 * Extend ID/Credential
\r
242 * This behavior will accelerate getting out of P1 outages due to ignoring renewal requests, or
\r
243 * other expiration issues.
\r
245 * Scenario is that people who are solving Password problems at night, are not necessarily those who
\r
246 * know what the passwords are supposed to be. Also, changing Password, without changing Configurations
\r
247 * using that password only exacerbates the P1 Issue.
\r
249 authzAPI.route(PUT,"/authn/cred/:days",API.CRED_REQ,new Code(facade,"Extend an ID/Credential", true) {
\r
251 public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
\r
252 Result<Void> r = context.extendUserCred(trans, req, pathParam(req, "days"));
\r
254 resp.setStatus(HttpStatus.OK_200);
\r
256 context.error(trans,resp,r);
\r
262 * Delete a ID/Credential by Object
\r
264 authzAPI.route(DELETE,"/authn/cred",API.CRED_REQ,new Code(facade,"Delete a Credential", true) {
\r
266 public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
\r
267 Result<Void> r = context.deleteUserCred(trans, req);
\r
269 resp.setStatus(HttpStatus.OK_200);
\r
271 context.error(trans,resp,r);
\r