|
| 1 | +/* |
| 2 | + * Copyright (c) 2014 ICM Uniwersytet Warszawski All rights reserved. |
| 3 | + * See LICENCE.txt file for licensing information. |
| 4 | + */ |
| 5 | +package pl.edu.icm.unity.saml.sp; |
| 6 | + |
| 7 | +import java.net.URL; |
| 8 | +import java.util.Optional; |
| 9 | + |
| 10 | +import org.apache.logging.log4j.Logger; |
| 11 | +import org.apache.xmlbeans.XmlException; |
| 12 | +import org.springframework.beans.factory.annotation.Autowired; |
| 13 | + |
| 14 | +import eu.unicore.samly2.SAMLBindings; |
| 15 | +import eu.unicore.samly2.validators.ReplayAttackChecker; |
| 16 | +import pl.edu.icm.unity.base.utils.Log; |
| 17 | +import pl.edu.icm.unity.engine.api.authn.AuthenticationResult; |
| 18 | +import pl.edu.icm.unity.engine.api.authn.AuthenticationResult.ResolvableError; |
| 19 | +import pl.edu.icm.unity.engine.api.authn.RemoteAuthenticationException; |
| 20 | +import pl.edu.icm.unity.engine.api.authn.RemoteAuthenticationResult; |
| 21 | +import pl.edu.icm.unity.engine.api.authn.remote.RedirectedAuthnState; |
| 22 | +import pl.edu.icm.unity.engine.api.authn.remote.RemoteAuthnResultTranslator; |
| 23 | +import pl.edu.icm.unity.engine.api.authn.remote.RemotelyAuthenticatedInput; |
| 24 | +import pl.edu.icm.unity.engine.api.endpoint.SharedEndpointManagement; |
| 25 | +import pl.edu.icm.unity.engine.api.server.AdvertisedAddressProvider; |
| 26 | +import pl.edu.icm.unity.engine.api.utils.PrototypeComponent; |
| 27 | +import pl.edu.icm.unity.saml.SAMLResponseValidatorUtil; |
| 28 | +import pl.edu.icm.unity.types.translation.TranslationProfile; |
| 29 | +import xmlbeans.org.oasis.saml2.protocol.ResponseDocument; |
| 30 | + |
| 31 | +@PrototypeComponent |
| 32 | +public class SAMLResponseVerificator |
| 33 | +{ |
| 34 | + private static final Logger log = Log.getLogger(Log.U_SERVER_SAML, SAMLResponseVerificator.class); |
| 35 | + private final String responseConsumerAddress; |
| 36 | + private final ReplayAttackChecker replayAttackChecker; |
| 37 | + private final RemoteAuthnResultTranslator translator; |
| 38 | + |
| 39 | + @Autowired |
| 40 | + public SAMLResponseVerificator( |
| 41 | + ReplayAttackChecker replayAttackChecker, |
| 42 | + SharedEndpointManagement sharedEndpointManagement, |
| 43 | + AdvertisedAddressProvider advertisedAddrProvider, |
| 44 | + RemoteAuthnResultTranslator translator) |
| 45 | + { |
| 46 | + this(replayAttackChecker, |
| 47 | + assembleResponseConsumerAddress(sharedEndpointManagement, advertisedAddrProvider), |
| 48 | + translator); |
| 49 | + } |
| 50 | + |
| 51 | + public SAMLResponseVerificator( |
| 52 | + ReplayAttackChecker replayAttackChecker, |
| 53 | + String responseConsumerAddress, |
| 54 | + RemoteAuthnResultTranslator translator) |
| 55 | + { |
| 56 | + this.replayAttackChecker = replayAttackChecker; |
| 57 | + this.translator = translator; |
| 58 | + this.responseConsumerAddress = responseConsumerAddress; |
| 59 | + } |
| 60 | + |
| 61 | + private static String assembleResponseConsumerAddress(SharedEndpointManagement sharedEndpointManagement, |
| 62 | + AdvertisedAddressProvider advertisedAddrProvider) |
| 63 | + { |
| 64 | + URL baseAddress = advertisedAddrProvider.get(); |
| 65 | + String baseContext = sharedEndpointManagement.getBaseContextPath(); |
| 66 | + return baseAddress + baseContext + SAMLResponseConsumerServlet.PATH; |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + AuthenticationResult processResponse(RedirectedAuthnState remoteAuthnState, TranslationProfile profile) |
| 71 | + { |
| 72 | + RemoteAuthnContext castedState = (RemoteAuthnContext) remoteAuthnState; |
| 73 | + try |
| 74 | + { |
| 75 | + return verifySAMLResponse(castedState, profile); |
| 76 | + } catch (Exception e) |
| 77 | + { |
| 78 | + log.error("Runtime error during SAML response processing or principal mapping", e); |
| 79 | + return RemoteAuthenticationResult.failed(null, e, |
| 80 | + new ResolvableError("WebSAMLRetrieval.authnFailedError")); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private AuthenticationResult verifySAMLResponse(RemoteAuthnContext context, TranslationProfile profile) |
| 85 | + { |
| 86 | + try |
| 87 | + { |
| 88 | + RemotelyAuthenticatedInput input = getRemotelyAuthenticatedInput(context); |
| 89 | + return translator.getTranslatedResult(input, profile, |
| 90 | + context.getAuthenticationTriggeringContext().isSandboxTriggered(), |
| 91 | + Optional.empty(), |
| 92 | + context.getRegistrationFormForUnknown(), |
| 93 | + context.isEnableAssociation()); |
| 94 | + } catch (RemoteAuthenticationException e) |
| 95 | + { |
| 96 | + log.info("SAML response verification or processing failed", e); |
| 97 | + return RemoteAuthenticationResult.failed(e.getResult().getRemotelyAuthenticatedPrincipal(), e, |
| 98 | + new ResolvableError("WebSAMLRetrieval.authnFailedError")); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private RemotelyAuthenticatedInput getRemotelyAuthenticatedInput(RemoteAuthnContext context) |
| 103 | + throws RemoteAuthenticationException |
| 104 | + { |
| 105 | + ResponseDocument responseDocument; |
| 106 | + try |
| 107 | + { |
| 108 | + responseDocument = ResponseDocument.Factory.parse(context.getResponse()); |
| 109 | + } catch (XmlException e) |
| 110 | + { |
| 111 | + throw new RemoteAuthenticationException("The SAML response can not be parsed - " + |
| 112 | + "XML data is corrupted", e); |
| 113 | + } |
| 114 | + |
| 115 | + SAMLResponseValidatorUtil responseValidatorUtil = new SAMLResponseValidatorUtil( |
| 116 | + context.getContextConfig(), replayAttackChecker, responseConsumerAddress); |
| 117 | + |
| 118 | + RemotelyAuthenticatedInput input = responseValidatorUtil.verifySAMLResponse(responseDocument, |
| 119 | + context.getVerifiableResponse(), |
| 120 | + context.getRequestId(), |
| 121 | + SAMLBindings.valueOf(context.getResponseBinding().toString()), |
| 122 | + context.getGroupAttribute(), context.getContextIdpKey()); |
| 123 | + return input; |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | + |
0 commit comments