-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
86 changed files
with
24,136 additions
and
5,301 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
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
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
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
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
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
20 changes: 20 additions & 0 deletions
20
...main/java/edu/internet2/tier/shibboleth/admin/ui/envers/PrincipalAwareRevisionEntity.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,20 @@ | ||
package edu.internet2.tier.shibboleth.admin.ui.envers; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.hibernate.envers.DefaultRevisionEntity; | ||
import org.hibernate.envers.RevisionEntity; | ||
|
||
import javax.persistence.Entity; | ||
|
||
/** | ||
* Extension of the default envers revision entity to track authenticated principals | ||
*/ | ||
@Entity | ||
@RevisionEntity(PrincipalEnhancingRevisionListener.class) | ||
@Getter | ||
@Setter | ||
public class PrincipalAwareRevisionEntity extends DefaultRevisionEntity { | ||
|
||
private String principalUserName; | ||
} |
20 changes: 20 additions & 0 deletions
20
...ava/edu/internet2/tier/shibboleth/admin/ui/envers/PrincipalEnhancingRevisionListener.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,20 @@ | ||
package edu.internet2.tier.shibboleth.admin.ui.envers; | ||
|
||
import org.hibernate.envers.RevisionListener; | ||
|
||
import static edu.internet2.tier.shibboleth.admin.ui.security.springsecurity.PrincipalAccessor.currentPrincipalIfLoggedIn; | ||
|
||
/** | ||
* Implementation of envers revision listener to enhance revision entity with authenticated principal username. | ||
*/ | ||
public class PrincipalEnhancingRevisionListener implements RevisionListener { | ||
|
||
private static final String ANONYMOUS = "anonymous"; | ||
|
||
@Override | ||
public void newRevision(Object revisionEntity) { | ||
PrincipalAwareRevisionEntity rev = (PrincipalAwareRevisionEntity) revisionEntity; | ||
String user = currentPrincipalIfLoggedIn().orElse(ANONYMOUS); | ||
rev.setPrincipalUserName(user); | ||
} | ||
} |
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
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
21 changes: 21 additions & 0 deletions
21
...ava/edu/internet2/tier/shibboleth/admin/ui/security/springsecurity/PrincipalAccessor.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,21 @@ | ||
package edu.internet2.tier.shibboleth.admin.ui.security.springsecurity; | ||
|
||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
|
||
import java.util.Optional; | ||
|
||
public final class PrincipalAccessor { | ||
|
||
//Non-instantiable utility class | ||
private PrincipalAccessor() { | ||
} | ||
|
||
public static Optional<String> currentPrincipalIfLoggedIn() { | ||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | ||
if (authentication == null) { | ||
return Optional.empty(); | ||
} | ||
return Optional.of(authentication.getName()); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
...n/java/edu/internet2/tier/shibboleth/admin/ui/service/FileCheckingFileWritingService.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,75 @@ | ||
package edu.internet2.tier.shibboleth.admin.ui.service; | ||
|
||
import org.springframework.core.io.WritableResource; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.security.DigestInputStream; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.Arrays; | ||
|
||
public class FileCheckingFileWritingService implements FileWritingService { | ||
private static final String DEFAULT_ALGORITHM = "MD5"; | ||
private final String algorithm; | ||
|
||
public FileCheckingFileWritingService() { | ||
this(DEFAULT_ALGORITHM); | ||
} | ||
|
||
public FileCheckingFileWritingService(String algorithm) { | ||
this.algorithm = algorithm; | ||
} | ||
|
||
@Override | ||
public void write(Path path, String content) throws IOException { | ||
if (Files.exists(path)) { | ||
try (InputStream is = Files.newInputStream(path)) { | ||
if (checkContentMatches(is, content)) { | ||
return; | ||
} | ||
} catch (NoSuchAlgorithmException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
writeContent(path, content); | ||
} | ||
|
||
@Override | ||
public void write(WritableResource resource, String content) throws IOException { | ||
if (resource.exists()) { | ||
try (InputStream is = resource.getInputStream()) { | ||
if (checkContentMatches(is, content)) { | ||
return; | ||
} | ||
} catch (NoSuchAlgorithmException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
writeContent(resource, content); | ||
} | ||
|
||
private boolean checkContentMatches(InputStream inputStream, String content) throws NoSuchAlgorithmException, IOException { | ||
MessageDigest md = MessageDigest.getInstance(this.algorithm); | ||
try (DigestInputStream dis = new DigestInputStream(inputStream, md)) { | ||
byte[] buf = new byte[4096]; | ||
while (dis.read(buf) > -1) {} | ||
} | ||
byte[] fileDigest = md.digest(); | ||
byte[] contentDigest = md.digest(content.getBytes()); | ||
return Arrays.equals(fileDigest, contentDigest); | ||
} | ||
|
||
void writeContent(Path path, String content) throws IOException { | ||
Files.write(path, content.getBytes()); | ||
} | ||
|
||
void writeContent(WritableResource resource, String content) throws IOException { | ||
try (OutputStream os = resource.getOutputStream()) { | ||
os.write(content.getBytes()); | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/service/FileWritingService.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,30 @@ | ||
package edu.internet2.tier.shibboleth.admin.ui.service; | ||
|
||
import org.springframework.core.io.WritableResource; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
|
||
/** | ||
* Service interface for writing files. Implementations may perform various tasks | ||
* before or after writing the file. | ||
*/ | ||
public interface FileWritingService { | ||
/** | ||
* Write content to a file | ||
* | ||
* @param path target file Path | ||
* @param content content to write | ||
* @throws IOException | ||
*/ | ||
void write(Path path, String content) throws IOException; | ||
|
||
/** | ||
* Write content to a writeable resource | ||
* | ||
* @param resource | ||
* @param content | ||
* @throws IOException | ||
*/ | ||
void write(WritableResource resource, String content) throws IOException; | ||
} |
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
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
Oops, something went wrong.