Skip to content

Commit 148f547

Browse files
committed
feat: extract elements from MVP sample
Refs: #4, #5
1 parent e447fb4 commit 148f547

File tree

7 files changed

+239
-48
lines changed

7 files changed

+239
-48
lines changed

examples/relying-party-spring-boot/src/main/java/it/spid/cie/oidc/spring/boot/relying/party/RelyingPartyWrapper.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.springframework.beans.factory.annotation.Autowired;
99
import org.springframework.stereotype.Component;
1010

11+
import it.spid.cie.oidc.callback.RelyingPartyLogoutCallback;
1112
import it.spid.cie.oidc.config.RelyingPartyOptions;
1213
import it.spid.cie.oidc.exception.OIDCException;
1314
import it.spid.cie.oidc.handler.RelyingPartyHandler;
@@ -39,6 +40,12 @@ public WellKnownData getWellKnownData(String requestURL, boolean jsonMode)
3940
return relyingPartyHandler.getWellKnownData(requestURL, jsonMode);
4041
}
4142

43+
public String performLogout(String userKey, RelyingPartyLogoutCallback callback)
44+
throws OIDCException {
45+
46+
return relyingPartyHandler.performLogout(userKey, callback);
47+
}
48+
4249
@PostConstruct
4350
private void postConstruct() throws OIDCException {
4451
RelyingPartyOptions options = new RelyingPartyOptions()

examples/relying-party-spring-boot/src/main/java/it/spid/cie/oidc/spring/boot/relying/party/controller/SpidController.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@
1818
import org.springframework.web.bind.annotation.RestController;
1919
import org.springframework.web.servlet.view.RedirectView;
2020

21+
import it.spid.cie.oidc.callback.RelyingPartyLogoutCallback;
22+
import it.spid.cie.oidc.model.AuthnRequest;
23+
import it.spid.cie.oidc.model.AuthnToken;
2124
import it.spid.cie.oidc.spring.boot.relying.party.RelyingPartyWrapper;
25+
import it.spid.cie.oidc.util.GetterUtil;
26+
import it.spid.cie.oidc.util.Validator;
2227

2328
@RestController
2429
@RequestMapping("/oidc/rp")
@@ -69,6 +74,34 @@ public RedirectView callback(
6974
return new RedirectView("echo_attributes");
7075
}
7176

77+
@GetMapping("/logout")
78+
public RedirectView logout(
79+
@RequestParam Map<String,String> params,
80+
final HttpServletRequest request, HttpServletResponse response)
81+
throws Exception {
82+
83+
String userKey = GetterUtil.getString(request.getSession().getAttribute("USER"));
84+
85+
String redirectURL = relyingPartyWrapper.performLogout(
86+
userKey, new RelyingPartyLogoutCallback() {
87+
88+
@Override
89+
public void logout(
90+
String userKey, AuthnRequest authnRequest, AuthnToken authnToken) {
91+
92+
request.getSession().removeAttribute("USER");
93+
request.getSession().removeAttribute("USER_INFO");
94+
}
95+
96+
});
97+
98+
if (!Validator.isNullOrEmpty(redirectURL)) {
99+
return new RedirectView(redirectURL);
100+
}
101+
102+
return new RedirectView("landing");
103+
}
104+
72105
private static Logger logger = LoggerFactory.getLogger(SpidController.class);
73106

74107
@Autowired

examples/relying-party-spring-boot/src/main/java/it/spid/cie/oidc/spring/boot/relying/party/persistence/H2PersistenceImpl.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.time.LocalDateTime;
44
import java.util.ArrayList;
55
import java.util.List;
6+
import java.util.Optional;
67

78
import org.slf4j.Logger;
89
import org.slf4j.LoggerFactory;
@@ -26,10 +27,29 @@
2627
import it.spid.cie.oidc.spring.boot.relying.party.persistence.model.FederationEntityRepository;
2728
import it.spid.cie.oidc.spring.boot.relying.party.persistence.model.TrustChainModel;
2829
import it.spid.cie.oidc.spring.boot.relying.party.persistence.model.TrustChainRepository;
30+
import it.spid.cie.oidc.util.GetterUtil;
2931

3032
@Component
3133
public class H2PersistenceImpl implements PersistenceAdapter {
3234

35+
@Override
36+
public AuthnRequest fetchAuthnRequest(String storageId) throws PersistenceException {
37+
try {
38+
long id = GetterUtil.getLong(storageId);
39+
40+
Optional<AuthnRequestModel> model = authnRequestRepository.findById(id);
41+
42+
if (model.isPresent()) {
43+
return model.get().toAuthnRequest();
44+
}
45+
}
46+
catch (Exception e) {
47+
throw new PersistenceException(e);
48+
}
49+
50+
return null;
51+
}
52+
3353
@Override
3454
public CachedEntityInfo fetchEntityInfo(String subject, String issuer)
3555
throws PersistenceException {
@@ -162,6 +182,24 @@ public List<AuthnRequest> findAuthnRequests(String state)
162182
}
163183
}
164184

185+
@Override
186+
public List<AuthnToken> findAuthnTokens(String userKey) throws PersistenceException {
187+
List<AuthnToken> result = new ArrayList<>();
188+
189+
try {
190+
List<AuthnTokenModel> models = authnTokenRepository.findUserTokens(userKey);
191+
192+
for (AuthnTokenModel model : models) {
193+
result.add(model.toAuthnToken());
194+
}
195+
196+
return result;
197+
}
198+
catch (Exception e) {
199+
throw new PersistenceException(e);
200+
}
201+
}
202+
165203
@Override
166204
public CachedEntityInfo storeEntityInfo(CachedEntityInfo entityInfo)
167205
throws PersistenceException {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package it.spid.cie.oidc.callback;
2+
3+
import it.spid.cie.oidc.model.AuthnRequest;
4+
import it.spid.cie.oidc.model.AuthnToken;
5+
6+
public interface RelyingPartyLogoutCallback {
7+
8+
public void logout(String userKey, AuthnRequest authnRequest, AuthnToken authnToken);
9+
10+
}

starter-kit/src/main/java/it/spid/cie/oidc/config/RelyingPartyOptions.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ public String getTrustMarks() {
7878
return trustMarks;
7979
}
8080

81+
public String getLoginURL() {
82+
return loginRedirectURL;
83+
}
84+
85+
public String getLogoutRedirectURL() {
86+
return logoutRedirectURL;
87+
}
88+
8189
public RelyingPartyOptions setProfileAcr(OIDCProfile profile, String acr) {
8290
if (acr != null) {
8391
if (OIDCProfile.SPID.equals(profile)) {

0 commit comments

Comments
 (0)