-
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
372e653
commit 781605a
Showing
10 changed files
with
191 additions
and
6 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
47 changes: 47 additions & 0 deletions
47
src/main/java/fr/recia/collabsoft/configuration/InterceptorConfig.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,47 @@ | ||
/* | ||
* Copyright (C) 2023 GIP-RECIA, 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 fr.recia.collabsoft.configuration; | ||
|
||
import fr.recia.collabsoft.interceptors.SoffitInterceptor; | ||
import fr.recia.collabsoft.interceptors.beans.SoffitHolder; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Scope; | ||
import org.springframework.context.annotation.ScopedProxyMode; | ||
import org.springframework.web.context.WebApplicationContext; | ||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
@Configuration | ||
public class InterceptorConfig implements WebMvcConfigurer { | ||
|
||
@Override | ||
public void addInterceptors(InterceptorRegistry registry) { | ||
registry.addInterceptor(soffitInterceptor()); | ||
} | ||
|
||
@Bean | ||
public SoffitInterceptor soffitInterceptor() { | ||
return new SoffitInterceptor(soffitHolder()); | ||
} | ||
|
||
@Bean | ||
@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS) | ||
public SoffitHolder soffitHolder() { | ||
return new SoffitHolder(); | ||
} | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
src/main/java/fr/recia/collabsoft/interceptors/SoffitInterceptor.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,63 @@ | ||
/* | ||
* Copyright (C) 2023 GIP-RECIA, 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 fr.recia.collabsoft.interceptors; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import fr.recia.collabsoft.interceptors.beans.SoffitHolder; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import java.util.Base64; | ||
import java.util.Map; | ||
|
||
@Slf4j | ||
public class SoffitInterceptor implements HandlerInterceptor { | ||
|
||
private final SoffitHolder soffitHolder; | ||
|
||
public SoffitInterceptor(SoffitHolder soffitHolder) { | ||
this.soffitHolder = soffitHolder; | ||
} | ||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { | ||
String token = request.getHeader("Authorization"); | ||
if (token == null) { | ||
log.debug("No Authorization header found"); | ||
soffitHolder.setSub(""); | ||
return true; | ||
} | ||
|
||
Base64.Decoder decoder = Base64.getUrlDecoder(); | ||
String payload = new String(decoder.decode(token.replace("Bearer ", "").split("\\.")[1])); | ||
|
||
Map<String, String> soffit = null; | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
try { | ||
soffit = objectMapper.readValue(payload, new TypeReference<>() { | ||
}); | ||
soffitHolder.setSub(soffit.get("sub")); | ||
} catch (IOException ignored) { | ||
} | ||
log.debug("Soffit : {}", soffit); | ||
return true; | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/fr/recia/collabsoft/interceptors/beans/SoffitHolder.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,25 @@ | ||
/* | ||
* Copyright (C) 2023 GIP-RECIA, 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 fr.recia.collabsoft.interceptors.beans; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class SoffitHolder { | ||
|
||
private String sub; | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import oidc, { type JWT } from '@uportal/open-id-connect'; | ||
|
||
const { VITE_USER_INFO_API_URI } = import.meta.env; | ||
|
||
const getToken = async (): Promise<{ encoded: string; decoded: JWT }> => { | ||
const { encoded, decoded } = await oidc({ userInfoApiUrl: VITE_USER_INFO_API_URI }); | ||
|
||
return { encoded, decoded }; | ||
}; | ||
|
||
export { getToken }; |
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