· 5 years ago · Oct 23, 2019, 09:00 AM
1public static Result loginSalesforceAuth(){
2
3 ObjectNode result = mapper.createObjectNode();
4 RequestId reqId = (RequestId) Context.current().args.get("_req");
5 String cname = (String) Context.current().args.get("_cname");
6
7 JsonNode body = request().body().asJson();
8 if(!body.has("auth_token") || !body.has("id_url") ) {
9 logger.info(reqId, "Either Auth token or Id URL is missing in the body. Please verify the request.");
10 return badRequest();
11 }
12
13 String auth_token = body.get("auth_token").asText();
14 String id_url = body.get("id_url").asText();
15
16 try {
17 id_url = id_url + "?oauth_token=" + auth_token;
18 RestUtils utils = new RestUtils("");
19 RestResponse restResponse = utils.doGet(id_url);
20
21 if(restResponse != null && restResponse.getStatus() == 200) {
22 logger.debug(reqId, restResponse.getResponse());
23 JsonNode node = mapper.readTree(restResponse.getResponse());
24 String uname = node.get("username").asText();
25 String email = node.get("email").asText();
26 String userId = node.get("user_id").asText();
27
28 Map<String, String> attributes = new HashMap<>();
29 attributes.put("email", email);
30 attributes.put("uid", userId);
31 attributes.put("uname", uname);
32
33 Learner learner = parseSSOAttributes(attributes);
34 result = getLoggedInJsonResp(reqId, learner);
35 return ok(result);
36
37 } else if(restResponse != null && restResponse.getStatus() == 403){
38 logger.info(reqId, restResponse.getResponse());
39 result.put("error", restResponse.getResponse());
40 return forbidden(result);
41 } else if(restResponse != null && restResponse.getStatus() == 404) {
42 logger.info(reqId, restResponse.getResponse());
43 result.put("error", restResponse.getResponse());
44 return notFound(result);
45 } else {
46 logger.info(reqId, "Invalid response from the server");
47 result.put("error", "Invalid response from the server");
48 return internalServerError(result);
49 }
50 } catch (Exception e) {
51 logger.error(reqId, e.getMessage(), e);
52 result.put("error", e.getMessage());
53 return internalServerError(result);
54 }
55 }