-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(lnurl-auth): set UserDetails as principal on successful authentic…
…ation
- Loading branch information
1 parent
6799cee
commit e82ca0b
Showing
8 changed files
with
284 additions
and
16 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
...-application/src/integTest/java/org/tbk/lightning/lnurl/example/AuthenticatedApiTest.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,69 @@ | ||
package org.tbk.lightning.lnurl.example; | ||
|
||
import kotlin.Pair; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.web.client.TestRestTemplate; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.RequestEntity; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.tbk.lnurl.auth.SignedLnurlAuth; | ||
import org.tbk.lnurl.test.SimpleLnurlWallet; | ||
|
||
import java.security.SecureRandom; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment; | ||
|
||
@SpringBootTest( | ||
webEnvironment = WebEnvironment.RANDOM_PORT, | ||
classes = LnurlAuthExampleApplication.class | ||
) | ||
@ActiveProfiles("test") | ||
class AuthenticatedApiTest { | ||
private static final SecureRandom random = new SecureRandom(); | ||
|
||
private static SimpleLnurlWallet testWallet; | ||
|
||
@Autowired | ||
private TestRestTemplate restTemplate; | ||
|
||
@BeforeAll | ||
static void setUpAll() { | ||
byte[] seed = random.generateSeed(256); | ||
testWallet = SimpleLnurlWallet.fromSeed(seed); | ||
} | ||
|
||
@Test | ||
void itShouldFetchAuthenticatedUserJson() { | ||
ResponseEntity<Object> request0 = restTemplate.exchange(RequestEntity | ||
.get("/api/v1/authenticated/self") | ||
.build(), Object.class); | ||
assertThat("user cannot see any guarded resource", request0.getStatusCode(), is(HttpStatus.FORBIDDEN)); | ||
|
||
Pair<SignedLnurlAuth, String> signedAuthAndSessionId = new LnurlAuthFlowTest.LnurlAuthFlowTestHelper(restTemplate, testWallet).login(); | ||
|
||
ResponseEntity<String> authTestRequest2ResponseEntity = restTemplate.exchange(RequestEntity | ||
.get("/api/v1/authenticated/self") | ||
.header(HttpHeaders.COOKIE, "SESSION=%s".formatted(signedAuthAndSessionId.getSecond())) | ||
.build(), String.class); | ||
assertThat(authTestRequest2ResponseEntity.getStatusCode(), is(HttpStatus.OK)); | ||
|
||
assertThat(authTestRequest2ResponseEntity.getBody(), is(""" | ||
{ | ||
"username" : "%s", | ||
"authorities" : [ { | ||
"authority" : "ROLE_USER" | ||
} ], | ||
"accountNonExpired" : true, | ||
"accountNonLocked" : true, | ||
"credentialsNonExpired" : true, | ||
"enabled" : true | ||
}""".formatted(signedAuthAndSessionId.getFirst().getLinkingKey().toHex()))); | ||
} | ||
} |
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
23 changes: 23 additions & 0 deletions
23
...ample-application/src/main/java/org/tbk/lightning/lnurl/example/api/AuthenticatedApi.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,23 @@ | ||
package org.tbk.lightning.lnurl.example.api; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/api/v1/authenticated") | ||
@RequiredArgsConstructor | ||
public class AuthenticatedApi { | ||
|
||
@GetMapping(path = "/self", produces = MediaType.APPLICATION_JSON_VALUE) | ||
public ResponseEntity<UserDetails> authenticationPrincipalUserDetails(@AuthenticationPrincipal(errorOnInvalidType = true) UserDetails currentUser) { | ||
return ResponseEntity.status(HttpStatus.OK) | ||
.body(currentUser); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -68,4 +68,4 @@ private static BufferedImage createImageWithText(String text) { | |
|
||
return bufferedImage; | ||
} | ||
} | ||
} |
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.