-
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
1 parent
ea62476
commit 6f88a6e
Showing
5 changed files
with
164 additions
and
19 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
61 changes: 61 additions & 0 deletions
61
src/main/java/nl/b3p/planmonitorwonen/api/model/auth/PlanmonitorAuthentication.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,61 @@ | ||
/* | ||
* Copyright (C) 2024 Provincie Zeeland | ||
* | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
package nl.b3p.planmonitorwonen.api.model.auth; | ||
|
||
import static org.springframework.http.HttpStatus.FORBIDDEN; | ||
import static org.springframework.http.HttpStatus.UNAUTHORIZED; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import nl.b3p.planmonitorwonen.api.security.TMAPIAuthenticationToken; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.web.server.ResponseStatusException; | ||
|
||
public class PlanmonitorAuthentication { | ||
private TMAPIAuthenticationToken tmApiAuthentication; | ||
private boolean isProvincie; | ||
private String gemeente; | ||
|
||
public TMAPIAuthenticationToken getTmApiAuthentication() { | ||
return tmApiAuthentication; | ||
} | ||
|
||
public boolean isProvincie() { | ||
return isProvincie; | ||
} | ||
|
||
public String getGemeente() { | ||
return gemeente; | ||
} | ||
|
||
public static PlanmonitorAuthentication getFromSecurityContext() throws ResponseStatusException { | ||
final TMAPIAuthenticationToken authentication = | ||
(TMAPIAuthenticationToken) SecurityContextHolder.getContext().getAuthentication(); | ||
if (authentication == null) { | ||
throw new ResponseStatusException(UNAUTHORIZED); | ||
} | ||
|
||
Map<String, String> groupProperties = new HashMap<>(); | ||
authentication | ||
.getAuthResponse() | ||
.get("groupProperties") | ||
.elements() | ||
.forEachRemaining( | ||
n -> groupProperties.put(n.get("key").textValue(), n.get("value").textValue())); | ||
|
||
PlanmonitorAuthentication result = new PlanmonitorAuthentication(); | ||
result.tmApiAuthentication = authentication; | ||
result.isProvincie = "provincie".equals(groupProperties.get("typeGebruiker")); | ||
result.gemeente = groupProperties.get("gemeente"); | ||
|
||
if (!result.isProvincie && result.gemeente == null) { | ||
throw new ResponseStatusException(FORBIDDEN); | ||
} | ||
|
||
return result; | ||
} | ||
} |
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