Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/one login jwt #10

Merged
merged 7 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package gov.cabinetoffice.gap.applybackend.dto.api;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class JwtPayloadV2 {
private String sub;
private String roles;
private String iss;
private String aud;
private int exp;
private int iat;
private String email;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package gov.cabinetoffice.gap.applybackend.dto.api;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class UserRolesJwtResponse {

//need jsonProperty because Jackson removes the 'is' from 'isValid'
@JsonProperty("isValid")
private boolean isValid;
@JsonProperty("isSuperAdmin")
private boolean isSuperAdmin;
@JsonProperty("isAdmin")
private boolean isAdmin;
@JsonProperty("isApplicant")
private boolean isApplicant;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.auth0.jwt.interfaces.DecodedJWT;
import gov.cabinetoffice.gap.applybackend.config.UserServiceConfig;
import gov.cabinetoffice.gap.applybackend.dto.api.JwtPayload;
import gov.cabinetoffice.gap.applybackend.dto.api.JwtPayloadV2;
import static java.lang.Boolean.TRUE;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -87,4 +88,25 @@ public JwtPayload decodeTheTokenPayloadInAReadableFormat(DecodedJWT jwt) {
.isAdmin(isAdmin)
.build();
}

public JwtPayloadV2 decodeTheTokenPayloadInAReadableFormatV2(DecodedJWT jwt) {
final String payloadJson = decodeBase64ToJson(jwt.getPayload());
final JSONObject jsonObject = new JSONObject(payloadJson);
final String sub = jwt.getSubject();
final String roles = jsonObject.getString("roles");
final String iss = jsonObject.getString("iss");
final String aud = jsonObject.getString("aud");
final int exp = jsonObject.getInt("exp");
final int iat = jsonObject.getInt("iat");
final String email = jsonObject.getString("email");
return JwtPayloadV2.builder()
.sub(sub)
.roles(roles)
.iss(iss)
.aud(aud)
.exp(exp)
.iat(iat)
.email(email)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import com.auth0.jwk.JwkException;
import com.auth0.jwt.interfaces.DecodedJWT;
import gov.cabinetoffice.gap.applybackend.dto.api.IsAdminJwtResponse;
import gov.cabinetoffice.gap.applybackend.dto.api.UserRolesJwtResponse;
import gov.cabinetoffice.gap.applybackend.dto.api.IsJwtValidResponse;
import gov.cabinetoffice.gap.applybackend.dto.api.JwtPayload;
import gov.cabinetoffice.gap.applybackend.dto.api.JwtPayloadV2;
import gov.cabinetoffice.gap.applybackend.exception.JwtTokenUndefinedException;
import gov.cabinetoffice.gap.applybackend.service.JwtService;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -76,4 +78,31 @@ public ResponseEntity<IsAdminJwtResponse> isAdmin(@RequestHeader("Authorization"

return ResponseEntity.ok(response);
}

@GetMapping("/userRoles")
public ResponseEntity<UserRolesJwtResponse> userRoles(@RequestHeader("Authorization") String jwtToken)
throws JwkException {

if (jwtToken.length() <= 0) {
throw new JwtTokenUndefinedException("No Jwt has been passed in the request");
}

final String normalisedJwt = jwtToken.split(" ")[1];
final DecodedJWT jwt = jwtService.decodedJwt(normalisedJwt);
final boolean isValid = jwtService.verifyToken(normalisedJwt);
final JwtPayloadV2 payload = jwtService.decodeTheTokenPayloadInAReadableFormatV2(jwt);

final boolean isSuperAdmin = payload.getRoles().contains("SUPER_ADMIN");
final boolean isAdmin = payload.getRoles().contains("ADMIN");
dominicwest marked this conversation as resolved.
Show resolved Hide resolved
final boolean isOrdinaryUser = payload.getRoles().contains("APPLICANT");

final UserRolesJwtResponse response = UserRolesJwtResponse.builder()
.isValid(isValid)
.isSuperAdmin(isSuperAdmin)
.isAdmin(isAdmin)
.isApplicant(isOrdinaryUser)
.build();

return ResponseEntity.ok(response);
dominicwest marked this conversation as resolved.
Show resolved Hide resolved
}
}