Skip to content

Commit 03a7dd4

Browse files
committed
Migrated to Java SDK, fixed project structure
1 parent 3e1713e commit 03a7dd4

File tree

6 files changed

+110
-69
lines changed

6 files changed

+110
-69
lines changed

complete/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
<groupId>org.springframework.boot</groupId>
2424
<artifactId>spring-boot-starter-web</artifactId>
2525
</dependency>
26+
<dependency>
27+
<groupId>com.corbado</groupId>
28+
<artifactId>corbado-java</artifactId>
29+
<version>0.0.1</version>
30+
</dependency>
2631
<dependency>
2732
<groupId>org.json</groupId>
2833
<artifactId>json</artifactId>
Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.corbado.springboot;
22

33
import java.util.Arrays;
4-
54
import org.springframework.boot.CommandLineRunner;
65
import org.springframework.boot.SpringApplication;
76
import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -11,23 +10,30 @@
1110
@SpringBootApplication
1211
public class Application {
1312

14-
public static void main(String[] args) {
15-
SpringApplication.run(Application.class, args);
16-
}
17-
18-
@Bean
19-
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
20-
return args -> {
21-
22-
System.out.println("Let's inspect the beans provided by Spring Boot:");
23-
24-
String[] beanNames = ctx.getBeanDefinitionNames();
25-
Arrays.sort(beanNames);
26-
for (String beanName : beanNames) {
27-
System.out.println(beanName);
28-
}
29-
30-
};
31-
}
32-
13+
/**
14+
* The main method.
15+
*
16+
* @param args the arguments
17+
*/
18+
public static void main(final String[] args) {
19+
SpringApplication.run(Application.class, args);
20+
}
21+
22+
/**
23+
* Command line runner.
24+
*
25+
* @param ctx the ctx
26+
* @return the command line runner
27+
*/
28+
@Bean
29+
public CommandLineRunner commandLineRunner(final ApplicationContext ctx) {
30+
return args -> {
31+
32+
final String[] beanNames = ctx.getBeanDefinitionNames();
33+
Arrays.sort(beanNames);
34+
for (final String beanName : beanNames) {
35+
System.out.println(beanName);
36+
}
37+
};
38+
}
3339
}
Lines changed: 77 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,91 @@
11
package com.corbado.springboot;
22

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;
810
import org.springframework.beans.factory.annotation.Value;
911
import org.springframework.stereotype.Controller;
1012
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;
1215

1316
@Controller
1417
public class FrontendController {
1518

16-
@Value("${projectid}")
17-
private String projectID;
19+
/** The project ID. */
20+
@Value("${projectID}")
21+
private String projectID;
1822

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());
2476

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());
2583

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";
6188
}
89+
return "profile";
90+
}
6291
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
projectid=pro-xxx
1+
projectID=pro-xxx
2+
apiSecret=corbado1_

0 commit comments

Comments
 (0)