|  | 
| 1 | 1 | package com.corbado.springboot; | 
| 2 | 2 | 
 | 
| 3 |  | -import com.nimbusds.jose.JWSVerifier; | 
| 4 |  | -import com.nimbusds.jose.crypto.RSASSAVerifier; | 
| 5 |  | -import com.nimbusds.jose.jwk.RSAKey; | 
| 6 |  | -import com.nimbusds.jwt.SignedJWT; | 
| 7 |  | -import org.json.JSONObject; | 
|  | 3 | +import com.corbado.entities.SessionValidationResult; | 
|  | 4 | +import com.corbado.exceptions.StandardException; | 
|  | 5 | +import com.corbado.generated.model.Identifier; | 
|  | 6 | +import com.corbado.sdk.Config; | 
|  | 7 | +import com.corbado.sdk.CorbadoSdk; | 
|  | 8 | +import java.util.List; | 
|  | 9 | +import org.springframework.beans.factory.annotation.Autowired; | 
| 8 | 10 | import org.springframework.beans.factory.annotation.Value; | 
| 9 | 11 | import org.springframework.stereotype.Controller; | 
| 10 | 12 | import org.springframework.ui.Model; | 
| 11 |  | -import org.springframework.web.bind.annotation.*; | 
|  | 13 | +import org.springframework.web.bind.annotation.CookieValue; | 
|  | 14 | +import org.springframework.web.bind.annotation.RequestMapping; | 
| 12 | 15 | 
 | 
| 13 | 16 | @Controller | 
| 14 | 17 | public class FrontendController { | 
| 15 | 18 | 
 | 
| 16 |  | -    @Value("${projectid}") | 
| 17 |  | -    private String projectID; | 
|  | 19 | +  /** The project ID. */ | 
|  | 20 | +  @Value("${projectID}") | 
|  | 21 | +  private String projectID; | 
| 18 | 22 | 
 | 
| 19 |  | -    @RequestMapping("/") | 
| 20 |  | -    public String index(Model model) { | 
| 21 |  | -        model.addAttribute("PROJECT_ID", projectID); | 
| 22 |  | -        return "index"; | 
| 23 |  | -    } | 
|  | 23 | +  /** The api secret. */ | 
|  | 24 | +  @Value("${apiSecret}") | 
|  | 25 | +  private String apiSecret; | 
|  | 26 | + | 
|  | 27 | +  /** The sdk. */ | 
|  | 28 | +  private final CorbadoSdk sdk; | 
|  | 29 | + | 
|  | 30 | +  /** | 
|  | 31 | +   * Index. | 
|  | 32 | +   * | 
|  | 33 | +   * @param model the model | 
|  | 34 | +   * @return the string | 
|  | 35 | +   * @throws StandardException | 
|  | 36 | +   */ | 
|  | 37 | +  @Autowired | 
|  | 38 | +  public FrontendController( | 
|  | 39 | +      @Value("${projectID}") final String projectID, @Value("${apiSecret}") final String apiSecret) | 
|  | 40 | +      throws StandardException { | 
|  | 41 | +    final Config config = new Config(projectID, apiSecret); | 
|  | 42 | +    this.sdk = new CorbadoSdk(config); | 
|  | 43 | +  } | 
|  | 44 | + | 
|  | 45 | +  /** | 
|  | 46 | +   * Index. | 
|  | 47 | +   * | 
|  | 48 | +   * @param model the model | 
|  | 49 | +   * @return the string | 
|  | 50 | +   */ | 
|  | 51 | +  @RequestMapping("/") | 
|  | 52 | +  public String index(final Model model) { | 
|  | 53 | +    model.addAttribute("PROJECT_ID", projectID); | 
|  | 54 | +    return "index"; | 
|  | 55 | +  } | 
|  | 56 | + | 
|  | 57 | +  /** | 
|  | 58 | +   * Profile. | 
|  | 59 | +   * | 
|  | 60 | +   * @param model the model | 
|  | 61 | +   * @param cboShortSession the cbo short session | 
|  | 62 | +   * @return the string | 
|  | 63 | +   */ | 
|  | 64 | +  @RequestMapping("/profile") | 
|  | 65 | +  public String profile( | 
|  | 66 | +      final Model model, @CookieValue("cbo_short_session") final String cboShortSession) { | 
|  | 67 | +    try { | 
|  | 68 | +      // Validate user from token | 
|  | 69 | + | 
|  | 70 | +      final SessionValidationResult validationResp = | 
|  | 71 | +          sdk.getSessions().getAndValidateCurrentUser(cboShortSession); | 
|  | 72 | +      // get list of emails from identifier service | 
|  | 73 | +      List<Identifier> emails; | 
|  | 74 | + | 
|  | 75 | +      emails = sdk.getIdentifiers().listAllEmailsByUserId(validationResp.getUserID()); | 
| 24 | 76 | 
 | 
|  | 77 | +      // | 
|  | 78 | +      model.addAttribute("PROJECT_ID", projectID); | 
|  | 79 | +      model.addAttribute("USER_ID", validationResp.getUserID()); | 
|  | 80 | +      model.addAttribute("USER_NAME", validationResp.getFullName()); | 
|  | 81 | +      // select email of your liking or list all emails | 
|  | 82 | +      model.addAttribute("USER_EMAIL", emails.get(0).getValue()); | 
| 25 | 83 | 
 | 
| 26 |  | -    @RequestMapping("/profile") | 
| 27 |  | -    public String profile(Model model, @CookieValue("cbo_short_session") String cboShortSession) { | 
| 28 |  | -        String issuer = "https://" + projectID + ".frontendapi.corbado.io"; | 
| 29 |  | -        String jwks_uri = "https://" + projectID + ".frontendapi.corbado.io/.well-known/jwks"; | 
| 30 |  | - | 
| 31 |  | -        try { | 
| 32 |  | -            JSONObject json = JsonReader.readJsonFromUrl(jwks_uri); | 
| 33 |  | -            JSONObject publicKey = json.getJSONArray("keys").getJSONObject(0); | 
| 34 |  | -            SignedJWT signedJWT = SignedJWT.parse(cboShortSession); | 
| 35 |  | -            RSAKey rsaKey = RSAKey.parse(publicKey.toString()); | 
| 36 |  | -            JWSVerifier verifier = new RSASSAVerifier(rsaKey); | 
| 37 |  | -            boolean isValid = signedJWT.verify(verifier); | 
| 38 |  | -            if (!isValid) { | 
| 39 |  | -                model.addAttribute("ERROR", "JWT token is not valid!"); | 
| 40 |  | -                return "error"; | 
| 41 |  | -            } | 
| 42 |  | - | 
| 43 |  | -            JSONObject payloadJSON = new JSONObject(signedJWT.getPayload().toJSONObject()); | 
| 44 |  | -            String kid = payloadJSON.getString("iss"); | 
| 45 |  | -            if (!kid.equals(issuer)) { | 
| 46 |  | -                model.addAttribute("ERROR", "JWT token issuer does not match!"); | 
| 47 |  | -                return "error"; | 
| 48 |  | -            } | 
| 49 |  | - | 
| 50 |  | -            model.addAttribute("PROJECT_ID", projectID); | 
| 51 |  | -            model.addAttribute("USER_ID", payloadJSON.get("sub")); | 
| 52 |  | -            model.addAttribute("USER_NAME", payloadJSON.get("name")); | 
| 53 |  | -            model.addAttribute("USER_EMAIL", payloadJSON.get("email")); | 
| 54 |  | -            return "profile"; | 
| 55 |  | - | 
| 56 |  | -        } catch (Exception e) { | 
| 57 |  | -            System.out.println(e.getMessage()); | 
| 58 |  | -            model.addAttribute("ERROR", e.getMessage()); | 
| 59 |  | -            return "error"; | 
| 60 |  | -        } | 
|  | 84 | +    } catch (final Exception e) { | 
|  | 85 | +      System.out.println(e.getMessage()); | 
|  | 86 | +      model.addAttribute("ERROR", e.getMessage()); | 
|  | 87 | +      return "error"; | 
| 61 | 88 |     } | 
|  | 89 | +    return "profile"; | 
|  | 90 | +  } | 
| 62 | 91 | } | 
0 commit comments