<commit_before>//  Copyright (c) 2015 Rackspace
//
//  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 proxyserver

import (
	"errors"
	"fmt"
	"net/http"
	"net/http/httptest"
	"strings"

	"github.com/openstack/swift/go/hummingbird"
)

type testUser struct {
	Account  string
	Username string
	Password string
	Roles    []string
	Url      string
}

type TempAuth struct {
	mc        hummingbird.MemcacheRing
	testUsers []testUser
	next      http.Handler
}

func NewTempAuth(mc hummingbird.MemcacheRing, config hummingbird.Config) func(http.Handler) http.Handler {
	var users []testUser
	// Hardcoding "tempauth" is incorrect. The actual name of the section
	// is determined by the entry in the pipeline= in [pipeline:main]. XXX
	section := "filter:tempauth"
	if config.HasSection(section) {
		users = []testUser{}
		for key, val := range config.File[section] {
			keyparts := strings.Split(key, "_")
			valparts := strings.Fields(val)
			if len(keyparts) != 3 || keyparts[0] != "user" {
				// egg specification and other configuration
				continue
			}
			vallen := len(valparts)
			if vallen < 1 {
				continue
			}
			url := ""
			groups := []string{}
			if vallen > 1 {
				urlSpot := 0
				s := valparts[vallen-1]
				if strings.HasPrefix(s, "http://") || strings.HasPrefix(s, "https://") {
					urlSpot = 1
					url = s
				}
				for _, group := range valparts[1:vallen-urlSpot] {
					groups = append(groups, group)
				}
			}
			user := testUser{keyparts[1], keyparts[2], valparts[0], groups, url}
			users = append(users, user)
		}
	} else {
		// Swift would traceback in case of a missing section, but
		// traditional Hummingbird uses a hardcoded tempauth. Always.
		// [XXX isn't it a very bad idea in a public cloud?]
		users = []testUser{
			{"admin", "admin", "admin", []string{".admin", ".reseller_admin"}, ""},
			{"test", "tester", "testing", []string{".admin"}, ""},
			{"test2", "tester2", "testing2", []string{".admin"}, ""},
			{"test2", "tester2", "testing2", []string{".admin"}, ""},
			{"test", "tester3", "testing3", []string{}, ""},
		}
	}
	tempAuth := &TempAuth{
		mc: mc,
		testUsers: users,
	}
	return tempAuth.getMiddleware
}

func (ta *TempAuth) getMiddleware(next http.Handler) http.Handler {
	ta.next = next
	return ta
}

func (ta *TempAuth) login(account, user, key string) (string, string, error) {
	for _, tu := range ta.testUsers {<commit_msg>Apply go fmt to tempauth<commit_after>//  Copyright (c) 2015 Rackspace
//
//  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 proxyserver

import (
	"errors"
	"fmt"
	"net/http"
	"net/http/httptest"
	"strings"

	"github.com/openstack/swift/go/hummingbird"
)

type testUser struct {
	Account  string
	Username string
	Password string
	Roles    []string
	Url      string
}

type TempAuth struct {
	mc        hummingbird.MemcacheRing
	testUsers []testUser
	next      http.Handler
}

func NewTempAuth(mc hummingbird.MemcacheRing, config hummingbird.Config) func(http.Handler) http.Handler {
	var users []testUser
	// Hardcoding "tempauth" is incorrect. The actual name of the section
	// is determined by the entry in the pipeline= in [pipeline:main]. XXX
	section := "filter:tempauth"
	if config.HasSection(section) {
		users = []testUser{}
		for key, val := range config.File[section] {
			keyparts := strings.Split(key, "_")
			valparts := strings.Fields(val)
			if len(keyparts) != 3 || keyparts[0] != "user" {
				// egg specification and other configuration
				continue
			}
			vallen := len(valparts)
			if vallen < 1 {
				continue
			}
			url := ""
			groups := []string{}
			if vallen > 1 {
				urlSpot := 0
				s := valparts[vallen-1]
				if strings.HasPrefix(s, "http://") || strings.HasPrefix(s, "https://") {
					urlSpot = 1
					url = s
				}
				for _, group := range valparts[1 : vallen-urlSpot] {
					groups = append(groups, group)
				}
			}
			user := testUser{keyparts[1], keyparts[2], valparts[0], groups, url}
			users = append(users, user)
		}
	} else {
		// Swift would traceback in case of a missing section, but
		// traditional Hummingbird uses a hardcoded tempauth. Always.
		// [XXX isn't it a very bad idea in a public cloud?]
		users = []testUser{
			{"admin", "admin", "admin", []string{".admin", ".reseller_admin"}, ""},
			{"test", "tester", "testing", []string{".admin"}, ""},
			{"test2", "tester2", "testing2", []string{".admin"}, ""},
			{"test2", "tester2", "testing2", []string{".admin"}, ""},
			{"test", "tester3", "testing3", []string{}, ""},
		}
	}
	tempAuth := &TempAuth{
		mc:        mc,
		testUsers: users,
	}
	return tempAuth.getMiddleware
}

func (ta *TempAuth) getMiddleware(next http.Handler) http.Handler {
	ta.next = next
	return ta
}

func (ta *TempAuth) login(account, user, key string) (string, string, error) {
	for _, tu := range ta.testUsers {
{'fraction_non_alphanumeric': 0.1430531732418525, 'fraction_numerical': 0.009262435677530018, 'mean_word_length': 3.3320950965824667, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '2981228', 'n_tokens_mistral': 2099, 'n_tokens_neox': 1892, 'n_words': 597}