Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ch4mpy committed Sep 16, 2022
1 parent e0b7226 commit 412a150
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ public class GreetingController {
@GetMapping()
@PreAuthorize("hasAuthority('NICE')")
public String getGreeting(ProxiesAuthentication auth) {
return String.format(
"Hi %s! You are granted with: %s and can proxy: %s.",
return "Hi %s! You are granted with: %s and can proxy: %s.".formatted(
auth.getName(),
auth.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.joining(", ", "[", "]")),
auth.getDetails().getProxies().keySet().stream().collect(Collectors.joining(", ", "[", "]")));
auth.getClaims().getProxies().keySet().stream().collect(Collectors.joining(", ", "[", "]")));
}

@GetMapping("/public")
Expand All @@ -32,6 +31,6 @@ public String getPublicGreeting() {
@GetMapping("/on-behalf-of/{username}")
@PreAuthorize("is(#username) or isNice() or onBehalfOf(#username).can('greet')")
public String getGreetingFor(@PathVariable("username") String username, Authentication auth) {
return String.format("Hi %s from %s!", username, auth.getName());
return "Hi %s from %s!".formatted(username, auth.getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class OAuthentication<T extends Map<String, Object> & Serializable> exten
private static final long serialVersionUID = -2827891205034221389L;

private final String tokenString;
private final T claims;

/**
* @param claims claim-set of any-type
Expand All @@ -46,19 +47,14 @@ public OAuthentication(T claims, Collection<? extends GrantedAuthority> authorit
super(authorities);
super.setAuthenticated(true);
super.setDetails(claims);
this.claims = claims;
this.tokenString = Optional.ofNullable(tokenString).map(ts -> ts.toLowerCase().startsWith("bearer ") ? ts.substring(7) : ts).orElse(null);
}

public OAuthentication(T claims, Converter<T, Collection<? extends GrantedAuthority>> authoritiesConverter, String tokenString) {
this(claims, authoritiesConverter.convert(claims), tokenString);
}

@SuppressWarnings("unchecked")
@Override
public T getDetails() {
return (T) super.getDetails();
}

@Override
public void setDetails(Object details) {
throw new RuntimeException("OAuthentication details are immutable");
Expand All @@ -71,21 +67,21 @@ public void setAuthenticated(boolean isAuthenticated) {

@Override
public T getCredentials() {
return getDetails();
return claims;
}

@Override
public T getPrincipal() {
return getDetails();
return claims;
}

@Override
public T getAttributes() {
return getDetails();
return claims;
}

public T getClaims() {
return getAttributes();
return claims;
}

public String getBearerHeader() {
Expand Down

0 comments on commit 412a150

Please sign in to comment.