Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
administration
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OZG-Cloud
app
administration
Commits
8d741f16
Commit
8d741f16
authored
1 year ago
by
Lukas Malte Monnerjahn
Browse files
Options
Downloads
Patches
Plain Diff
OZG-5176 reintroduce WithJwt
parent
078acd12
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/test/java/de/ozgcloud/admin/security/SecurityConfigurationITCase.java
+8
-3
8 additions, 3 deletions
.../ozgcloud/admin/security/SecurityConfigurationITCase.java
src/test/java/de/ozgcloud/admin/security/WithJwt.java
+111
-0
111 additions, 0 deletions
src/test/java/de/ozgcloud/admin/security/WithJwt.java
with
119 additions
and
3 deletions
src/test/java/de/ozgcloud/admin/security/SecurityConfigurationITCase.java
+
8
−
3
View file @
8d741f16
...
...
@@ -137,10 +137,15 @@ class SecurityConfigurationITCase {
@DisplayName
(
"with authentication"
)
@Nested
class
TestWithAuthentication
{
static
final
String
CLAIMS
=
"""
{
"preferredUsername": "testUser",
"scope": "openid testscope"
}"""
;
@Test
@SneakyThrows
@With
MockUser
@With
Jwt
(
CLAIMS
)
void
shouldAllowApiEndpoint
()
{
var
result
=
doPerformAuthenticated
(
"/api"
);
...
...
@@ -149,7 +154,7 @@ class SecurityConfigurationITCase {
@Test
@SneakyThrows
@With
MockUser
@With
Jwt
(
CLAIMS
)
void
shouldForbidSettingsEndpoint
()
{
var
result
=
doPerformAuthenticated
(
"/api/configuration/settings"
);
...
...
@@ -158,7 +163,7 @@ class SecurityConfigurationITCase {
@Test
@SneakyThrows
@With
MockUser
@With
Jwt
(
CLAIMS
)
void
shouldForbidConfigurationsEndpoint
()
{
var
result
=
doPerformAuthenticated
(
"/api/configuration"
);
...
...
This diff is collapsed.
Click to expand it.
src/test/java/de/ozgcloud/admin/security/WithJwt.java
0 → 100644
+
111
−
0
View file @
8d741f16
/*
* Copyright (c) 2024. Das Land Schleswig-Holstein vertreten durch das Ministerium für Energiewende, Klimaschutz, Umwelt und Natur
* Zentrales IT-Management
*
* Lizenziert unter der EUPL, Version 1.2 oder - sobald
* diese von der Europäischen Kommission genehmigt wurden -
* Folgeversionen der EUPL ("Lizenz");
* Sie dürfen dieses Werk ausschließlich gemäß
* dieser Lizenz nutzen.
* Eine Kopie der Lizenz finden Sie hier:
*
* https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
*
* Sofern nicht durch anwendbare Rechtsvorschriften
* gefordert oder in schriftlicher Form vereinbart, wird
* die unter der Lizenz verbreitete Software "so wie sie
* ist", OHNE JEGLICHE GEWÄHRLEISTUNG ODER BEDINGUNGEN -
* ausdrücklich oder stillschweigend - verbreitet.
* Die sprachspezifischen Genehmigungen und Beschränkungen
* unter der Lizenz sind dem Lizenztext zu entnehmen.
*/
package
de.ozgcloud.admin.security
;
import
java.lang.annotation.Documented
;
import
java.lang.annotation.ElementType
;
import
java.lang.annotation.Inherited
;
import
java.lang.annotation.Retention
;
import
java.lang.annotation.RetentionPolicy
;
import
java.lang.annotation.Target
;
import
java.time.Instant
;
import
java.util.Map
;
import
java.util.Optional
;
import
org.springframework.core.convert.converter.Converter
;
import
org.springframework.security.authentication.AbstractAuthenticationToken
;
import
org.springframework.security.core.Authentication
;
import
org.springframework.security.core.context.SecurityContext
;
import
org.springframework.security.core.context.SecurityContextHolder
;
import
org.springframework.security.oauth2.jwt.Jwt
;
import
org.springframework.security.test.context.support.WithSecurityContext
;
import
org.springframework.security.test.context.support.WithSecurityContextFactory
;
import
org.springframework.util.StringUtils
;
import
com.nimbusds.jwt.JWTClaimNames
;
import
lombok.RequiredArgsConstructor
;
import
net.minidev.json.JSONObject
;
import
net.minidev.json.parser.JSONParser
;
import
net.minidev.json.parser.ParseException
;
/**
* Annotation to setup test {@link SecurityContext} with an {@link Authentication}. Adjusted from source:
* com.c4_soft.springaddons.security.oauth2.test.annotations.WithJwt Author: Jérôme Wacongne <ch4mp@c4-soft.com>
*/
@Target
({
ElementType
.
METHOD
,
ElementType
.
TYPE
})
@Retention
(
RetentionPolicy
.
RUNTIME
)
@Inherited
@Documented
@WithSecurityContext
(
factory
=
WithJwt
.
AuthenticationFactory
.
class
)
public
@interface
WithJwt
{
String
value
()
default
""
;
String
bearerString
()
default
AuthenticationFactory
.
DEFAULT_BEARER
;
String
headers
()
default
AuthenticationFactory
.
DEFAULT_HEADERS
;
@RequiredArgsConstructor
final
class
AuthenticationFactory
implements
WithSecurityContextFactory
<
WithJwt
>
{
static
final
String
DEFAULT_BEARER
=
"test.jwt.bearer"
;
static
final
String
DEFAULT_HEADERS
=
"{\"alg\": \"none\"}"
;
private
final
Converter
<
Jwt
,
?
extends
AbstractAuthenticationToken
>
jwtAuthenticationConverter
;
@Override
public
SecurityContext
createSecurityContext
(
WithJwt
annotation
)
{
var
auth
=
authentication
(
annotation
);
var
securityContext
=
SecurityContextHolder
.
createEmptyContext
();
securityContext
.
setAuthentication
(
auth
);
return
securityContext
;
}
private
AbstractAuthenticationToken
authentication
(
WithJwt
annotation
)
{
var
claims
=
parseJson
(
annotation
.
value
());
var
headers
=
parseJson
(
annotation
.
headers
());
var
bearerString
=
annotation
.
bearerString
();
var
now
=
Instant
.
now
();
var
iat
=
Optional
.
ofNullable
((
Integer
)
claims
.
get
(
JWTClaimNames
.
ISSUED_AT
)).
map
(
Instant:
:
ofEpochSecond
).
orElse
(
now
);
var
exp
=
Optional
.
ofNullable
((
Integer
)
claims
.
get
(
JWTClaimNames
.
EXPIRATION_TIME
)).
map
(
Instant:
:
ofEpochSecond
)
.
orElse
(
now
.
plusSeconds
(
42
));
var
jwt
=
new
Jwt
(
bearerString
,
iat
,
exp
,
headers
,
claims
);
return
jwtAuthenticationConverter
.
convert
(
jwt
);
}
private
static
Map
<
String
,
Object
>
parseJson
(
String
json
)
{
if
(!
StringUtils
.
hasText
(
json
))
{
return
Map
.
of
();
}
try
{
return
new
JSONParser
(
JSONParser
.
MODE_PERMISSIVE
).
parse
(
json
,
JSONObject
.
class
);
}
catch
(
final
ParseException
e
)
{
throw
new
RuntimeException
(
"Invalid JSON payload in @WithJwt"
);
}
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment