Skip to content

Commit

Permalink
feat: extract elements from MVP sample
Browse files Browse the repository at this point in the history
Refs: #4, #5
  • Loading branch information
mmariuzzo committed Mar 30, 2022
1 parent e447fb4 commit 148f547
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import it.spid.cie.oidc.callback.RelyingPartyLogoutCallback;
import it.spid.cie.oidc.config.RelyingPartyOptions;
import it.spid.cie.oidc.exception.OIDCException;
import it.spid.cie.oidc.handler.RelyingPartyHandler;
Expand Down Expand Up @@ -39,6 +40,12 @@ public WellKnownData getWellKnownData(String requestURL, boolean jsonMode)
return relyingPartyHandler.getWellKnownData(requestURL, jsonMode);
}

public String performLogout(String userKey, RelyingPartyLogoutCallback callback)
throws OIDCException {

return relyingPartyHandler.performLogout(userKey, callback);
}

@PostConstruct
private void postConstruct() throws OIDCException {
RelyingPartyOptions options = new RelyingPartyOptions()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.view.RedirectView;

import it.spid.cie.oidc.callback.RelyingPartyLogoutCallback;
import it.spid.cie.oidc.model.AuthnRequest;
import it.spid.cie.oidc.model.AuthnToken;
import it.spid.cie.oidc.spring.boot.relying.party.RelyingPartyWrapper;
import it.spid.cie.oidc.util.GetterUtil;
import it.spid.cie.oidc.util.Validator;

@RestController
@RequestMapping("/oidc/rp")
Expand Down Expand Up @@ -69,6 +74,34 @@ public RedirectView callback(
return new RedirectView("echo_attributes");
}

@GetMapping("/logout")
public RedirectView logout(
@RequestParam Map<String,String> params,
final HttpServletRequest request, HttpServletResponse response)
throws Exception {

String userKey = GetterUtil.getString(request.getSession().getAttribute("USER"));

String redirectURL = relyingPartyWrapper.performLogout(
userKey, new RelyingPartyLogoutCallback() {

@Override
public void logout(
String userKey, AuthnRequest authnRequest, AuthnToken authnToken) {

request.getSession().removeAttribute("USER");
request.getSession().removeAttribute("USER_INFO");
}

});

if (!Validator.isNullOrEmpty(redirectURL)) {
return new RedirectView(redirectURL);
}

return new RedirectView("landing");
}

private static Logger logger = LoggerFactory.getLogger(SpidController.class);

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -26,10 +27,29 @@
import it.spid.cie.oidc.spring.boot.relying.party.persistence.model.FederationEntityRepository;
import it.spid.cie.oidc.spring.boot.relying.party.persistence.model.TrustChainModel;
import it.spid.cie.oidc.spring.boot.relying.party.persistence.model.TrustChainRepository;
import it.spid.cie.oidc.util.GetterUtil;

@Component
public class H2PersistenceImpl implements PersistenceAdapter {

@Override
public AuthnRequest fetchAuthnRequest(String storageId) throws PersistenceException {
try {
long id = GetterUtil.getLong(storageId);

Optional<AuthnRequestModel> model = authnRequestRepository.findById(id);

if (model.isPresent()) {
return model.get().toAuthnRequest();
}
}
catch (Exception e) {
throw new PersistenceException(e);
}

return null;
}

@Override
public CachedEntityInfo fetchEntityInfo(String subject, String issuer)
throws PersistenceException {
Expand Down Expand Up @@ -162,6 +182,24 @@ public List<AuthnRequest> findAuthnRequests(String state)
}
}

@Override
public List<AuthnToken> findAuthnTokens(String userKey) throws PersistenceException {
List<AuthnToken> result = new ArrayList<>();

try {
List<AuthnTokenModel> models = authnTokenRepository.findUserTokens(userKey);

for (AuthnTokenModel model : models) {
result.add(model.toAuthnToken());
}

return result;
}
catch (Exception e) {
throw new PersistenceException(e);
}
}

@Override
public CachedEntityInfo storeEntityInfo(CachedEntityInfo entityInfo)
throws PersistenceException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package it.spid.cie.oidc.callback;

import it.spid.cie.oidc.model.AuthnRequest;
import it.spid.cie.oidc.model.AuthnToken;

public interface RelyingPartyLogoutCallback {

public void logout(String userKey, AuthnRequest authnRequest, AuthnToken authnToken);

}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ public String getTrustMarks() {
return trustMarks;
}

public String getLoginURL() {
return loginRedirectURL;
}

public String getLogoutRedirectURL() {
return logoutRedirectURL;
}

public RelyingPartyOptions setProfileAcr(OIDCProfile profile, String acr) {
if (acr != null) {
if (OIDCProfile.SPID.equals(profile)) {
Expand Down
Loading

0 comments on commit 148f547

Please sign in to comment.