<commit_before>// Copyright 2015 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package saml

import (
	"fmt"
	"time"

	saml "github.com/diego-araujo/go-saml"
	"github.com/tsuru/config"
	"github.com/tsuru/tsuru/errors"
)

var (
	ErrRequestIdNotFound = &errors.ValidationError{Message: "Field attribute InResponseTo not found in saml response data"}
	ErrCheckSignature    = &errors.ValidationError{Message: "SAMLResponse signature validation"}
)

type response struct {
	ID       string    `json:"id"`
	Creation time.Time `json:"creation"`
}

func getRequestIdFromResponse(r *saml.Response) (string, error) {
	var idRequest string
	if r.IsEncrypted() {
		idRequest = r.EncryptedAssertion.Assertion.Subject.SubjectConfirmation.SubjectConfirmationData.InResponseTo
	} else {
		idRequest = r.Assertion.Subject.SubjectConfirmation.SubjectConfirmationData.InResponseTo
	}
	if idRequest == "" {
		return "", ErrRequestIdNotFound
	}
	return idRequest, nil
}

func getUserIdentity(r *saml.Response) (string, error) {
	attrFriendlyNameIdentifier, err := config.GetString("auth:saml:idp-attribute-user-identity")
	if err != nil {
		return "", fmt.Errorf("error reading config auth:saml:idp-attribute-user-identity: %s ", err)
	}
	userIdentifier := r.GetAttribute(attrFriendlyNameIdentifier)
	if userIdentifier == "" {
		return "", fmt.Errorf("unable to parse identity provider data - not found  <Attribute FriendlyName="+attrFriendlyNameIdentifier+"> ")
	}
	return userIdentifier, nil
}

func validateResponse(r *saml.Response, sp *saml.ServiceProviderSettings) error {
	err := r.Validate(sp)
	if err != nil {
		return err
	}
	if sp.IDPSignResponse {
		err := r.ValidateResponseSignature(sp)
		if err != nil {
			return err
		}
	}
	err = r.ValidateExpiredConfirmation(sp)<commit_msg>Fix typos and review coding standards<commit_after>// Copyright 2015 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package saml

import (
	"fmt"
	"time"

	saml "github.com/diego-araujo/go-saml"
	"github.com/tsuru/config"
	"github.com/tsuru/tsuru/errors"
)

var (
	ErrRequestIdNotFound = &errors.ValidationError{Message: "Field attribute InResponseTo not found in saml response data"}
	ErrCheckSignature    = &errors.ValidationError{Message: "SAMLResponse signature validation"}
)

type response struct {
	ID       string    `json:"id"`
	Creation time.Time `json:"creation"`
}

func getRequestIdFromResponse(r *saml.Response) (string, error) {
	var idRequest string
	if r.IsEncrypted() {
		idRequest = r.EncryptedAssertion.Assertion.Subject.SubjectConfirmation.SubjectConfirmationData.InResponseTo
	} else {
		idRequest = r.Assertion.Subject.SubjectConfirmation.SubjectConfirmationData.InResponseTo
	}
	if idRequest == "" {
		return "", ErrRequestIdNotFound
	}
	return idRequest, nil
}

func getUserIdentity(r *saml.Response) (string, error) {
	attrFriendlyNameIdentifier, err := config.GetString("auth:saml:idp-attribute-user-identity")
	if err != nil {
		return "", fmt.Errorf("error reading config auth:saml:idp-attribute-user-identity: %s ", err)
	}
	userIdentifier := r.GetAttribute(attrFriendlyNameIdentifier)
	if userIdentifier == "" {
		return "", fmt.Errorf("unable to parse identity provider data - not found  <Attribute FriendlyName=" + attrFriendlyNameIdentifier + "> ")
	}
	return userIdentifier, nil
}

func validateResponse(r *saml.Response, sp *saml.ServiceProviderSettings) error {
	err := r.Validate(sp)
	if err != nil {
		return err
	}
	if sp.IDPSignResponse {
		err := r.ValidateResponseSignature(sp)
		if err != nil {
			return err
		}
	}
	err = r.ValidateExpiredConfirmation(sp)
{'fraction_non_alphanumeric': 0.11170212765957446, 'fraction_numerical': 0.002127659574468085, 'mean_word_length': 4.7684049079754605, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '4400568', 'n_tokens_mistral': 1219, 'n_tokens_neox': 1189, 'n_words': 293}