-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce universal/security library for AuthN and AuthZ
commit-id:17aa4dbb
- Loading branch information
1 parent
e8c2163
commit 46b0f23
Showing
34 changed files
with
2,194 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...iversal/src/main/java/com/pinterest/teletraan/universal/security/AnonymousAuthFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* Copyright (c) 2024 Pinterest, Inc. | ||
* | ||
* 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. | ||
*/ | ||
package com.pinterest.teletraan.universal.security; | ||
|
||
import com.pinterest.teletraan.universal.security.bean.AnonymousUser; | ||
import java.io.IOException; | ||
import java.security.Principal; | ||
import javax.annotation.Priority; | ||
import javax.ws.rs.Priorities; | ||
import javax.ws.rs.container.ContainerRequestContext; | ||
import javax.ws.rs.container.ContainerRequestFilter; | ||
import javax.ws.rs.core.SecurityContext; | ||
import javax.ws.rs.ext.Provider; | ||
|
||
/** | ||
* A filter for authenticating and authorizing any request as an anonymous user. For development use | ||
* only. | ||
*/ | ||
@Provider | ||
@Priority(Priorities.AUTHENTICATION) | ||
public class AnonymousAuthFilter implements ContainerRequestFilter { | ||
public static final AnonymousUser USER = new AnonymousUser(); | ||
private SecurityContext securityContext; | ||
|
||
public AnonymousAuthFilter() { | ||
securityContext = | ||
new SecurityContext() { | ||
|
||
@Override | ||
public Principal getUserPrincipal() { | ||
return AnonymousAuthFilter.USER; | ||
} | ||
|
||
@Override | ||
public boolean isUserInRole(String s) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isSecure() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public String getAuthenticationScheme() { | ||
return "Anonymous"; | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public void filter(ContainerRequestContext containerRequestContext) throws IOException { | ||
containerRequestContext.setSecurityContext(securityContext); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...niversal/src/main/java/com/pinterest/teletraan/universal/security/AuditLoggingFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* Copyright (c) 2024 Pinterest, Inc. | ||
* | ||
* 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. | ||
*/ | ||
package com.pinterest.teletraan.universal.security; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import javax.annotation.Priority; | ||
import javax.ws.rs.container.ContainerRequestContext; | ||
import javax.ws.rs.container.ContainerResponseContext; | ||
import javax.ws.rs.container.ContainerResponseFilter; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
/** | ||
* A filter for logging audit information for each request. Should be configured differently than | ||
* regular application logs. | ||
*/ | ||
@Priority(Integer.MIN_VALUE) | ||
@Slf4j | ||
public class AuditLoggingFilter implements ContainerResponseFilter { | ||
private static final String PRINCIPAL = "principal"; | ||
private static final String RESOURCE = "resource"; | ||
private static final String METHOD = "method"; | ||
private static final String STATUS = "status"; | ||
|
||
@Override | ||
public void filter( | ||
ContainerRequestContext requestContext, ContainerResponseContext responseContext) | ||
throws IOException { | ||
if (requestContext.getSecurityContext() == null | ||
|| requestContext.getSecurityContext().getUserPrincipal() == null) { | ||
return; | ||
} | ||
Map<String, Object> attributes = new HashMap<>(); | ||
try { | ||
attributes.put( | ||
PRINCIPAL, requestContext.getSecurityContext().getUserPrincipal().getName()); | ||
attributes.put(RESOURCE, requestContext.getUriInfo().getRequestUri().toString()); | ||
attributes.put(METHOD, requestContext.getMethod()); | ||
attributes.put(STATUS, responseContext.getStatus()); | ||
|
||
String json = new ObjectMapper().writeValueAsString(attributes); | ||
log.info(json); | ||
} catch (Exception ex) { | ||
log.error("Failed to generate audit log for: {}", attributes, ex); | ||
} | ||
} | ||
} |
Oops, something went wrong.