<commit_before>// Copyright © 2017 <NAME> <<EMAIL>>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package sp

import (
	"bytes"
	"encoding/xml"
	"errors"
	"fmt"
	"io"
	"net/http"
	"os"
	"time"

	"github.com/amdonov/lite-idp/saml"
	log "github.com/sirupsen/logrus"
)

type ArtifactCallback func(w http.ResponseWriter, r *http.Request, state []byte, assertion *saml.Assertion)

func (sp *serviceProvider) ArtifactFunc(callback ArtifactCallback) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		err := r.ParseForm()
		if err != nil {
			log.Error(err)
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		// retrieve the relayState from our cache
		stateID := r.Form.Get("RelayState")
		if stateID == "" {
			log.Info("received a request without RelayState")
			http.Error(w, "identity provider did not return RelayState", http.StatusUnauthorized)
			return
		}
		state, err := sp.stateCache.Get(stateID)
		if err != nil {
			log.Info("RelayState not found in cache")
			http.Error(w, "identity provider did not return RelayState", http.StatusUnauthorized)
			return
		}

		// call the IdP to get the SAML assertion
		artifact := r.Form.Get("SAMLart")

		assertion, err := sp.resolveArtifact(artifact)
		if err != nil {
			http.Error(w, err.Error(), http.StatusUnauthorized)
			return
		}

		// allow the application to write the response
		callback(w, r, state, assertion)
	}
}

func (sp *serviceProvider) resolveArtifact(artifact string) (*saml.Assertion, error) {
	request, err := sp.buildResolveRequest(artifact)
	if err != nil {
		return nil, err
	}
	post, err := http.NewRequest(http.MethodPost, sp.configuration.IDPArtifactEndpoint, request)
	if err != nil {
		return nil, err
	}
	post.Header.Add("Content-Type", "text/xml")
	post.Header.Add("SOAPAction", "http://www.oasis-open.org/committees/security")
	resp, err := sp.client.Do(post)
	if err != nil {
		return nil, err
	}
	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("unexpected status code from artifact resolve request %d", resp.StatusCode)
	}
	defer resp.Body.Close()
	decoder := xml.NewDecoder(resp.Body)
	response := &saml.ArtifactResponseEnvelope{}
	err = decoder.Decode(response)
	if err != nil {
		return nil, err
	}
	assertion := response.Body.ArtifactResponse.Response.Assertion
	if assertion == nil {<commit_msg>Moved close above status check<commit_after>// Copyright © 2017 <NAME> <<EMAIL>>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package sp

import (
	"bytes"
	"encoding/xml"
	"errors"
	"fmt"
	"io"
	"net/http"
	"os"
	"time"

	"github.com/amdonov/lite-idp/saml"
	log "github.com/sirupsen/logrus"
)

type ArtifactCallback func(w http.ResponseWriter, r *http.Request, state []byte, assertion *saml.Assertion)

func (sp *serviceProvider) ArtifactFunc(callback ArtifactCallback) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		err := r.ParseForm()
		if err != nil {
			log.Error(err)
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		// retrieve the relayState from our cache
		stateID := r.Form.Get("RelayState")
		if stateID == "" {
			log.Info("received a request without RelayState")
			http.Error(w, "identity provider did not return RelayState", http.StatusUnauthorized)
			return
		}
		state, err := sp.stateCache.Get(stateID)
		if err != nil {
			log.Info("RelayState not found in cache")
			http.Error(w, "identity provider did not return RelayState", http.StatusUnauthorized)
			return
		}

		// call the IdP to get the SAML assertion
		artifact := r.Form.Get("SAMLart")

		assertion, err := sp.resolveArtifact(artifact)
		if err != nil {
			http.Error(w, err.Error(), http.StatusUnauthorized)
			return
		}

		// allow the application to write the response
		callback(w, r, state, assertion)
	}
}

func (sp *serviceProvider) resolveArtifact(artifact string) (*saml.Assertion, error) {
	request, err := sp.buildResolveRequest(artifact)
	if err != nil {
		return nil, err
	}
	post, err := http.NewRequest(http.MethodPost, sp.configuration.IDPArtifactEndpoint, request)
	if err != nil {
		return nil, err
	}
	post.Header.Add("Content-Type", "text/xml")
	post.Header.Add("SOAPAction", "http://www.oasis-open.org/committees/security")
	resp, err := sp.client.Do(post)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("unexpected status code from artifact resolve request %d", resp.StatusCode)
	}
	decoder := xml.NewDecoder(resp.Body)
	response := &saml.ArtifactResponseEnvelope{}
	err = decoder.Decode(response)
	if err != nil {
		return nil, err
	}
	assertion := response.Body.ArtifactResponse.Response.Assertion
	if assertion == nil {
{'fraction_non_alphanumeric': 0.12423873325213154, 'fraction_numerical': 0.002784061249347486, 'mean_word_length': 3.942390369733448, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5581285', 'n_tokens_mistral': 1956, 'n_tokens_neox': 1823, 'n_words': 558}