Skip to content

Commit

Permalink
Add LoginController.loginCallback
Browse files Browse the repository at this point in the history
For now, validates response state against a dummy state (and hence
fails)
  • Loading branch information
sortraev committed Feb 11, 2025
1 parent 491931a commit ada1297
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/main/java/no/idporten/example/login/LoginController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package no.idporten.example.login;

import com.nimbusds.oauth2.sdk.*;
import com.nimbusds.oauth2.sdk.id.State;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.net.URI;

@Controller
public class LoginController {

// used to check state in authorization responses.
private State lastState = new State(); // TODO: dummy.

@GetMapping(path = "/login")
public String login() {
return "";
}

@GetMapping(path = "/callback")
public String loginCallback(HttpServletRequest req, Model model) {
URI callbackURI = URI.create(req.getRequestURL() + "?" + req.getQueryString());
return handleCallbackURI(callbackURI, model);
}

private String handleCallbackURI(URI callbackURI, Model model) {
AuthorizationResponse resp;
try {
resp = AuthorizationResponse.parse(callbackURI);
}
catch (ParseException e) {
model.addAttribute("errmsg_attr", "Authorization response parse error");
return "login_fail";
}

if (!resp.indicatesSuccess()) {
model.addAttribute("errmsg_attr", resp.toErrorResponse());
return "login_fail";
}

if (!resp.getState().equals(lastState)) {
model.addAttribute("errmsg_attr", "Bad state");
return "login_fail";
}

model.addAttribute("errmsg_attr", "Success");

AuthorizationCode authzCode = resp.toSuccessResponse()
.getAuthorizationCode();
model.addAttribute("authz_code_attr", authzCode.getValue());

return "login_success";
}
}

0 comments on commit ada1297

Please sign in to comment.