gopkg.in/goose.v2@v2.0.1/testservices/identityservice/legacy.go (about)

     1  package identityservice
     2  
     3  import (
     4  	"net/http"
     5  )
     6  
     7  type Legacy struct {
     8  	Users
     9  	managementURL string
    10  }
    11  
    12  func NewLegacy() *Legacy {
    13  	service := &Legacy{}
    14  	service.users = make(map[string]UserInfo)
    15  	service.tenants = make(map[string]string)
    16  	return service
    17  }
    18  
    19  func (lis *Legacy) RegisterServiceProvider(name, serviceType string, serviceProvider ServiceProvider) {
    20  	// NOOP for legacy identity service.
    21  }
    22  
    23  func (lis *Legacy) AddService(service Service) {
    24  	// NOOP for legacy identity service.
    25  }
    26  
    27  func (lis *Legacy) SetManagementURL(URL string) {
    28  	lis.managementURL = URL
    29  }
    30  
    31  // setupHTTP attaches all the needed handlers to provide the HTTP API.
    32  func (lis *Legacy) SetupHTTP(mux *http.ServeMux) {
    33  	mux.Handle("/", lis)
    34  }
    35  
    36  func (lis *Legacy) Stop() {
    37  	// NOOP for legacy identity service.
    38  }
    39  
    40  func (lis *Legacy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    41  	username := r.Header.Get("X-Auth-User")
    42  	userInfo, ok := lis.users[username]
    43  	if !ok {
    44  		w.WriteHeader(http.StatusUnauthorized)
    45  		return
    46  	}
    47  	auth_key := r.Header.Get("X-Auth-Key")
    48  	if auth_key != userInfo.secret {
    49  		w.WriteHeader(http.StatusUnauthorized)
    50  		return
    51  	}
    52  	if userInfo.Token == "" {
    53  		userInfo.Token = randomHexToken()
    54  		lis.users[username] = userInfo
    55  	}
    56  	header := w.Header()
    57  	header.Set("X-Auth-Token", userInfo.Token)
    58  	header.Set("X-Server-Management-Url", lis.managementURL+"/compute")
    59  	header.Set("X-Storage-Url", lis.managementURL+"/object-store")
    60  	w.WriteHeader(http.StatusNoContent)
    61  }