-
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
d443c62
commit d379af4
Showing
31 changed files
with
1,811 additions
and
8 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
64 changes: 64 additions & 0 deletions
64
...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,64 @@ | ||
/** | ||
* 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; | ||
|
||
@Provider | ||
@Priority(Priorities.AUTHENTICATION) | ||
public class AnonymousAuthFilter implements ContainerRequestFilter { | ||
private 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 true; | ||
} | ||
|
||
@Override | ||
public String getAuthenticationScheme() { | ||
return "Anonymous"; | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public void filter(ContainerRequestContext containerRequestContext) throws IOException { | ||
containerRequestContext.setSecurityContext(securityContext); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...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,58 @@ | ||
/** | ||
* 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; | ||
|
||
@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); | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...rsal/src/main/java/com/pinterest/teletraan/universal/security/AuthZResourceExtractor.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,58 @@ | ||
/** | ||
* 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.AuthZResource; | ||
import javax.ws.rs.container.ContainerRequestContext; | ||
|
||
public interface AuthZResourceExtractor { | ||
/** | ||
* Extracts AuthZResource from the requestContext. | ||
* | ||
* @param requestContext | ||
* @return | ||
* @throws ExtractionException if no resource can be extracted | ||
*/ | ||
AuthZResource extractResource(ContainerRequestContext requestContext) | ||
throws ExtractionException; | ||
|
||
/** | ||
* Extracts AuthZResource from the requestContext. | ||
* | ||
* @param requestContext | ||
* @param beanClass is the class of the expected resource bean | ||
* @return | ||
* @throws ExtractionException if no resource can be extracted | ||
*/ | ||
default AuthZResource extractResource( | ||
ContainerRequestContext requestContext, Class<?> beanClass) throws ExtractionException { | ||
return extractResource(requestContext); | ||
} | ||
|
||
interface Factory { | ||
AuthZResourceExtractor create(ResourceAuthZInfo authZInfo); | ||
} | ||
|
||
class ExtractionException extends Exception { | ||
public ExtractionException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
public ExtractionException(String message) { | ||
super(message); | ||
} | ||
} | ||
} |
Oops, something went wrong.