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 2a8ba95 commit e447fb4
Show file tree
Hide file tree
Showing 14 changed files with 1,191 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import javax.annotation.PostConstruct;

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -26,6 +27,12 @@ public String getAuthorizeURL(
spidProvider, trustAnchor, redirectUri, scope, profile, prompt);
}

public JSONObject getUserInfo(String state, String code)
throws OIDCException {

return relyingPartyHandler.getUserInfo(state, code);
}

public WellKnownData getWellKnownData(String requestURL, boolean jsonMode)
throws OIDCException {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package it.spid.cie.oidc.spring.boot.relying.party.controller;

import java.net.URI;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -11,6 +16,7 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.view.RedirectView;

import it.spid.cie.oidc.spring.boot.relying.party.RelyingPartyWrapper;

Expand Down Expand Up @@ -39,6 +45,30 @@ public ResponseEntity<Void> authorize(
.build();
}

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

if (params.containsKey("error")) {
logger.error(new JSONObject(params).toString(2));

throw new Exception("TODO: Manage Error callback");
}

String state = params.get("state");
String code = params.get("code");

JSONObject userInfo = relyingPartyWrapper.getUserInfo(state, code);

request.getSession().setAttribute(
"USER", userInfo.optString("https://attributes.spid.gov.it/email"));
request.getSession().setAttribute("USER_INFO", userInfo.toMap());

return new RedirectView("echo_attributes");
}

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

@Autowired
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package it.spid.cie.oidc.spring.boot.relying.party.persistence;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -9,12 +11,15 @@

import it.spid.cie.oidc.exception.PersistenceException;
import it.spid.cie.oidc.model.AuthnRequest;
import it.spid.cie.oidc.model.AuthnToken;
import it.spid.cie.oidc.model.CachedEntityInfo;
import it.spid.cie.oidc.model.FederationEntity;
import it.spid.cie.oidc.model.TrustChain;
import it.spid.cie.oidc.persistence.PersistenceAdapter;
import it.spid.cie.oidc.spring.boot.relying.party.persistence.model.AuthnRequestModel;
import it.spid.cie.oidc.spring.boot.relying.party.persistence.model.AuthnRequestRepository;
import it.spid.cie.oidc.spring.boot.relying.party.persistence.model.AuthnTokenModel;
import it.spid.cie.oidc.spring.boot.relying.party.persistence.model.AuthnTokenRepository;
import it.spid.cie.oidc.spring.boot.relying.party.persistence.model.EntityInfoModel;
import it.spid.cie.oidc.spring.boot.relying.party.persistence.model.EntityInfoRepository;
import it.spid.cie.oidc.spring.boot.relying.party.persistence.model.FederationEntityModel;
Expand Down Expand Up @@ -137,6 +142,26 @@ public TrustChain fetchTrustChain(
return null;
}

@Override
public List<AuthnRequest> findAuthnRequests(String state)
throws PersistenceException {

List<AuthnRequest> result = new ArrayList<>();

try {
List<AuthnRequestModel> models = authnRequestRepository.findByState(state);

for (AuthnRequestModel model : models) {
result.add(model.toAuthnRequest());
}

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

@Override
public CachedEntityInfo storeEntityInfo(CachedEntityInfo entityInfo)
throws PersistenceException {
Expand Down Expand Up @@ -197,6 +222,26 @@ public AuthnRequest storeOIDCAuthnRequest(AuthnRequest authnRequest)
}
}

@Override
public AuthnToken storeOIDCAuthnToken(AuthnToken authnToken)
throws PersistenceException {

try {
AuthnTokenModel model = AuthnTokenModel.of(authnToken);

if (model.getId() != null && model.getId() > 0) {
model.setModified(LocalDateTime.now());
}

model = authnTokenRepository.save(model);

return model.toAuthnToken();
}
catch (Exception e) {
throw new PersistenceException(e);
}
}

@Override
public TrustChain storeTrustChain(TrustChain trustChain) throws PersistenceException {
try {
Expand Down Expand Up @@ -224,6 +269,9 @@ public TrustChain storeTrustChain(TrustChain trustChain) throws PersistenceExcep
@Autowired
private AuthnRequestRepository authnRequestRepository;

@Autowired
private AuthnTokenRepository authnTokenRepository;

@Autowired
private EntityInfoRepository entityInfoRepository;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ protected void setId(String storageId) {
}

private String getStorageId() {
if (id > 0) {
if (id != null && id > 0) {
return String.valueOf(id);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
package it.spid.cie.oidc.spring.boot.relying.party.persistence.model;

import java.time.LocalDateTime;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import it.spid.cie.oidc.model.AuthnToken;
import it.spid.cie.oidc.util.GetterUtil;
import it.spid.cie.oidc.util.Validator;

@Entity
@Table(name = "oidc_authentication_token")
public class AuthnTokenModel {

public static AuthnTokenModel of(AuthnToken source) {
AuthnTokenModel target = new AuthnTokenModel();

target.setId(source.getStorageId());
target.setCreated(source.getCreateDate());
target.setModified(source.getModifiedDate());
target.setAccessToken(source.getAccessToken());
target.setAuthzRequestId(source.getAuthnRequestId());
target.setCode(source.getCode());
target.setExpiresIn(source.getExpiresIn());
target.setIdToken(source.getIdToken());
target.setRefreshToken(source.getRefreshToken());
target.setRevoked(source.getRevoked());
target.setScope(source.getScope());
target.setTokenType(source.getTokenType());
target.setUserKey(source.getUserKey());

return target;
}

public AuthnTokenModel() {
this.created = LocalDateTime.now();
this.modified = this.created;
}

public Long getId() {
return id;
}

public LocalDateTime getCreated() {
return created;
}

public LocalDateTime getModified() {
return modified;
}

public String getAccessToken() {
return accessToken;
}

public long getAuthzRequestId() {
return authzRequestId;
}

public String getCode() {
return code;
}

public int getExpiresIn() {
return expiresIn;
}

public String getIdToken() {
return idToken;
}

public String getRefreshToken() {
return refreshToken;
}

public LocalDateTime getRevoked() {
return revoked;
}

public String getScope() {
return scope;
}

public String getTokenType() {
return tokenType;
}

public String getUserKey() {
return userKey;
}

public void setId(Long id) {
this.id = id;
}

public void setCreated(LocalDateTime created) {
this.created = created;
}

public void setModified(LocalDateTime modified) {
this.modified = modified;
}

public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}

public void setAuthzRequestId(long authzRequestId) {
this.authzRequestId = authzRequestId;
}

public void setCode(String code) {
this.code = code;
}

public void setExpiresIn(int expiresIn) {
this.expiresIn = expiresIn;
}

public void setIdToken(String idToken) {
this.idToken = idToken;
}

public void setRefreshToken(String refreshToken) {
this.refreshToken = refreshToken;
}

public void setRevoked(LocalDateTime revoked) {
this.revoked = revoked;
}

public void setScope(String scope) {
this.scope = scope;
}

public void setTokenType(String tokenType) {
this.tokenType = tokenType;
}

public void setUserKey(String userKey) {
this.userKey = userKey;
}

public AuthnToken toAuthnToken() {
AuthnToken target = new AuthnToken();

target.setStorageId(getStorageId());
target.setCreateDate(getCreated());
target.setModifiedDate(getModified());
target.setAccessToken(getAccessToken());
target.setAuthnRequestId(String.valueOf(getAuthzRequestId()));
target.setCode(getCode());
target.setExpiresIn(getExpiresIn());
target.setIdToken(getIdToken());
target.setRefreshToken(getRefreshToken());
target.setRevoked(getRevoked());
target.setScope(getScope());
target.setTokenType(getTokenType());
target.setUserKey(getUserKey());

return target;
}

protected void setAuthzRequestId(String authnRequestId) {
setAuthzRequestId(GetterUtil.getLong(authnRequestId));
}

protected void setId(String storageId) {
if (!Validator.isNullOrEmpty(storageId)) {
setId(GetterUtil.getLong(storageId));
}
}

private String getStorageId() {
if (id != null && id > 0) {
return String.valueOf(id);
}

return null;
}

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(nullable = false)
private LocalDateTime created;

@Column(nullable = false)
private LocalDateTime modified;

@Column(nullable = true)
private String code;

@Column(name = "access_token", nullable = true)
private String accessToken;

@Column(name = "id_token", nullable = true)
private String idToken;

@Column(nullable = true)
private String scope;

@Column(name = "token_type", nullable = true)
private String tokenType;

@Column(name = "expires_in", nullable = true)
private int expiresIn;

@Column(name = "authz_request_id", nullable = false)
private long authzRequestId;

@Column(name = "user_key", nullable = true)
private String userKey;

@Column(nullable = true)
private LocalDateTime revoked;

@Column(name = "refresh_token", nullable = true)
private String refreshToken;


}
Loading

0 comments on commit e447fb4

Please sign in to comment.