Search in sources :

Example 1 with AppsSAML20Details

use of org.maxkey.entity.apps.AppsSAML20Details in project MaxKey by dromara.

the class SAML20DetailsController method forwardUpdate.

@RequestMapping(value = { "/forwardUpdate/{id}" })
public ModelAndView forwardUpdate(@PathVariable("id") String id) {
    ModelAndView modelAndView = new ModelAndView("apps/saml20/appUpdate");
    AppsSAML20Details saml20Details = saml20DetailsService.getAppDetails(id, false);
    decoderSecret(saml20Details);
    saml20Details.transIconBase64();
    modelAndView.addObject("model", saml20Details);
    modelAndView.addObject("authzURI", applicationConfig.getAuthzUri());
    return modelAndView;
}
Also used : AppsSAML20Details(org.maxkey.entity.apps.AppsSAML20Details) ModelAndView(org.springframework.web.servlet.ModelAndView) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with AppsSAML20Details

use of org.maxkey.entity.apps.AppsSAML20Details in project MaxKey by dromara.

the class AssertionEndpoint method assertion.

@RequestMapping(value = "/authz/saml20/assertion")
public ModelAndView assertion(HttpServletRequest request, HttpServletResponse response) throws Exception {
    logger.debug("saml20 assertion start.");
    bindingAdapter = (BindingAdapter) request.getSession().getAttribute(WebConstants.AUTHORIZE_SIGN_ON_APP_SAMLV20_ADAPTER);
    logger.debug("saml20 assertion get session samlv20Adapter " + bindingAdapter);
    AppsSAML20Details saml20Details = bindingAdapter.getSaml20Details();
    logger.debug("saml20Details " + saml20Details.getExtendAttr());
    AuthnRequestInfo authnRequestInfo = bindingAdapter.getAuthnRequestInfo();
    if (authnRequestInfo == null) {
        logger.warn("Could not find AuthnRequest on the request.  Responding with SC_FORBIDDEN.");
        throw new Exception();
    }
    logger.debug("AuthnRequestInfo: {}", authnRequestInfo);
    HashMap<String, String> attributeMap = new HashMap<String, String>();
    attributeMap.put(WebConstants.ONLINE_TICKET_NAME, ((SigninPrincipal) WebContext.getAuthentication().getPrincipal()).getOnlineTicket().getTicketId());
    // saml20Details
    Response authResponse = authnResponseGenerator.generateAuthnResponse(saml20Details, authnRequestInfo, attributeMap, bindingAdapter);
    Endpoint endpoint = endpointGenerator.generateEndpoint(saml20Details.getSpAcsUrl());
    request.getSession().removeAttribute(AuthnRequestInfo.class.getName());
    // request issuer...
    try {
        bindingAdapter.sendSAMLMessage(authResponse, endpoint, request, response);
    } catch (MessageEncodingException mee) {
        logger.error("Exception encoding SAML message", mee);
        throw new Exception(mee);
    }
    return null;
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.opensaml.saml2.core.Response) AppsSAML20Details(org.maxkey.entity.apps.AppsSAML20Details) Endpoint(org.opensaml.saml2.metadata.Endpoint) HashMap(java.util.HashMap) AuthnRequestInfo(org.maxkey.authz.saml.common.AuthnRequestInfo) SigninPrincipal(org.maxkey.authn.SigninPrincipal) MessageEncodingException(org.opensaml.ws.message.encoder.MessageEncodingException) MessageEncodingException(org.opensaml.ws.message.encoder.MessageEncodingException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with AppsSAML20Details

use of org.maxkey.entity.apps.AppsSAML20Details in project MaxKey by dromara.

the class IdpInitEndpoint method authorizeIdpInit.

/**
 * @param request
 * @param response
 * @param appId
 * @return
 * @throws Exception
 */
@Operation(summary = "SAML 2.0 IDP Init接口", description = "传递参数应用ID", method = "GET")
@RequestMapping(value = "/authz/saml20/idpinit/{appid}", method = RequestMethod.GET)
public ModelAndView authorizeIdpInit(HttpServletRequest request, HttpServletResponse response, @PathVariable("appid") String appId) throws Exception {
    logger.debug("SAML IDP init , app id is " + appId);
    AppsSAML20Details saml20Details = saml20DetailsService.getAppDetails(appId, true);
    WebContext.setAttribute(WebConstants.AUTHORIZE_SIGN_ON_APP, saml20Details);
    if (saml20Details == null) {
        logger.error("samlId[" + appId + "] Error .");
        throw new Exception();
    }
    KeyStore trustKeyStore = KeyStoreUtil.bytes2KeyStore(saml20Details.getKeyStore(), keyStoreLoader.getKeyStore().getType(), keyStoreLoader.getKeystorePassword());
    extractRedirectBindingAdapter.setSaml20Detail(saml20Details);
    extractRedirectBindingAdapter.buildSecurityPolicyResolver(trustKeyStore);
    String binding = saml20Details.getBinding();
    if (binding.endsWith("PostSimpleSign")) {
        bindingAdapter = postSimpleSignBindingAdapter;
    } else {
        bindingAdapter = postBindingAdapter;
    }
    // AuthnRequestInfo init authnRequestID to null
    bindingAdapter.setAuthnRequestInfo(new AuthnRequestInfo());
    bindingAdapter.setExtractBindingAdapter(extractRedirectBindingAdapter);
    request.getSession().setAttribute(WebConstants.AUTHORIZE_SIGN_ON_APP_SAMLV20_ADAPTER, bindingAdapter);
    logger.debug("idp init forwarding to assertion :", "/authz/saml20/assertion");
    return WebContext.forward("/authz/saml20/assertion");
}
Also used : AppsSAML20Details(org.maxkey.entity.apps.AppsSAML20Details) AuthnRequestInfo(org.maxkey.authz.saml.common.AuthnRequestInfo) KeyStore(java.security.KeyStore) Operation(io.swagger.v3.oas.annotations.Operation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with AppsSAML20Details

use of org.maxkey.entity.apps.AppsSAML20Details in project MaxKey by dromara.

the class SingleSignOnEndpoint method extractSaml20Detail.

public void extractSaml20Detail(ExtractBindingAdapter extractBindingAdapter, String samlId) throws Exception {
    AppsSAML20Details saml20Details = saml20DetailsService.getAppDetails(samlId, true);
    WebContext.setAttribute(WebConstants.AUTHORIZE_SIGN_ON_APP, saml20Details);
    if (saml20Details == null) {
        logger.error("Request SAML APPID [" + samlId + "] is not exist .");
        throw new Exception();
    }
    KeyStore trustKeyStore = KeyStoreUtil.bytes2KeyStore(saml20Details.getKeyStore(), extractBindingAdapter.getKeyStoreLoader().getKeyStore().getType(), extractBindingAdapter.getKeyStoreLoader().getKeystorePassword());
    extractBindingAdapter.setSaml20Detail(saml20Details);
    extractBindingAdapter.buildSecurityPolicyResolver(trustKeyStore);
}
Also used : AppsSAML20Details(org.maxkey.entity.apps.AppsSAML20Details) KeyStore(java.security.KeyStore) MessageDecodingException(org.opensaml.ws.message.decoder.MessageDecodingException) ValidationException(org.opensaml.xml.validation.ValidationException) SecurityException(org.opensaml.xml.security.SecurityException)

Example 5 with AppsSAML20Details

use of org.maxkey.entity.apps.AppsSAML20Details in project MaxKey by dromara.

the class ConsumerEndpoint method initCredential.

/**
 * 初始化sp证书
 *
 * @throws Exception
 */
private void initCredential(String spId) throws Exception {
    // 1. 获取 sp keyStore
    AppsSAML20Details saml20Details = saml20DetailsService.get(spId);
    if (saml20Details == null) {
        logger.error("spid[" + spId + "] not exists");
        throw new Exception();
    }
    byte[] keyStoreBytes = saml20Details.getKeyStore();
    InputStream keyStoreStream = new ByteArrayInputStream(keyStoreBytes);
    try {
        KeyStore keyStore = KeyStore.getInstance(keyStoreLoader.getKeystoreType());
        keyStore.load(keyStoreStream, keyStoreLoader.getKeystorePassword().toCharArray());
        Map<String, String> passwords = new HashMap<String, String>();
        for (Enumeration<String> en = keyStore.aliases(); en.hasMoreElements(); ) {
            String aliase = en.nextElement();
            if (aliase.equalsIgnoreCase(keyStoreLoader.getEntityName())) {
                passwords.put(aliase, keyStoreLoader.getKeystorePassword());
            }
        }
        // TrustResolver trustResolver = new
        // TrustResolver(keyStore,keyStoreLoader.getIdpIssuingEntityName(),keyStoreLoader.getKeystorePassword());
        AuthnResponseGenerator authnResponseGenerator = new AuthnResponseGenerator(keyStoreLoader.getEntityName(), timeService, idService);
        // endpointGenerator = new EndpointGenerator();
        CriteriaSet criteriaSet = new CriteriaSet();
        criteriaSet.add(new EntityIDCriteria(keyStoreLoader.getEntityName()));
        criteriaSet.add(new UsageCriteria(UsageType.SIGNING));
        KeyStoreCredentialResolver credentialResolver = new KeyStoreCredentialResolver(keyStore, passwords);
        signingCredential = credentialResolver.resolveSingle(criteriaSet);
        Validate.notNull(signingCredential);
        // adapter set resolver
        TrustResolver trustResolver = new TrustResolver(keyStore, keyStoreLoader.getEntityName(), keyStoreLoader.getKeystorePassword(), issueInstantRule, messageReplayRule, "POST");
        extractBindingAdapter.setSecurityPolicyResolver(trustResolver.getStaticSecurityPolicyResolver());
    } catch (Exception e) {
        logger.error("初始化sp证书出错");
        throw new Exception(e);
    }
}
Also used : AppsSAML20Details(org.maxkey.entity.apps.AppsSAML20Details) AuthnResponseGenerator(org.maxkey.authz.saml20.provider.xml.AuthnResponseGenerator) EntityIDCriteria(org.opensaml.xml.security.criteria.EntityIDCriteria) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) KeyStore(java.security.KeyStore) MessageDecodingException(org.opensaml.ws.message.decoder.MessageDecodingException) IdentityProviderAuthenticationException(org.maxkey.authz.saml20.consumer.spring.IdentityProviderAuthenticationException) ValidationException(org.opensaml.xml.validation.ValidationException) SecurityException(org.opensaml.xml.security.SecurityException) ServiceProviderAuthenticationException(org.maxkey.authz.saml20.consumer.spring.ServiceProviderAuthenticationException) ByteArrayInputStream(java.io.ByteArrayInputStream) UsageCriteria(org.opensaml.xml.security.criteria.UsageCriteria) CriteriaSet(org.opensaml.xml.security.CriteriaSet) KeyStoreCredentialResolver(org.opensaml.xml.security.credential.KeyStoreCredentialResolver) TrustResolver(org.maxkey.authz.saml.common.TrustResolver)

Aggregations

AppsSAML20Details (org.maxkey.entity.apps.AppsSAML20Details)7 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 KeyStore (java.security.KeyStore)3 HashMap (java.util.HashMap)2 AuthnRequestInfo (org.maxkey.authz.saml.common.AuthnRequestInfo)2 MessageDecodingException (org.opensaml.ws.message.decoder.MessageDecodingException)2 SecurityException (org.opensaml.xml.security.SecurityException)2 ValidationException (org.opensaml.xml.validation.ValidationException)2 ModelAndView (org.springframework.web.servlet.ModelAndView)2 Operation (io.swagger.v3.oas.annotations.Operation)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 SigninPrincipal (org.maxkey.authn.SigninPrincipal)1 TrustResolver (org.maxkey.authz.saml.common.TrustResolver)1 IdentityProviderAuthenticationException (org.maxkey.authz.saml20.consumer.spring.IdentityProviderAuthenticationException)1 ServiceProviderAuthenticationException (org.maxkey.authz.saml20.consumer.spring.ServiceProviderAuthenticationException)1 AuthnResponseGenerator (org.maxkey.authz.saml20.provider.xml.AuthnResponseGenerator)1 Response (org.opensaml.saml2.core.Response)1 Endpoint (org.opensaml.saml2.metadata.Endpoint)1