~ubuntu-branches/ubuntu/vivid/juju-core/vivid-proposed

« back to all changes in this revision

Viewing changes to src/launchpad.net/goose/testservices/identityservice/keypair.go

  • Committer: Package Import Robot
  • Author(s): Curtis C. Hovey
  • Date: 2015-09-29 19:43:29 UTC
  • mfrom: (47.1.4 wily-proposed)
  • Revision ID: package-import@ubuntu.com-20150929194329-9y496tbic30hc7vp
Tags: 1.24.6-0ubuntu1~15.04.1
Backport of 1.24.6 from wily. (LP: #1500916, #1497087)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package identityservice
2
 
 
3
 
import (
4
 
        "encoding/json"
5
 
        "fmt"
6
 
        "io/ioutil"
7
 
        "launchpad.net/goose/testservices/hook"
8
 
        "net/http"
9
 
)
10
 
 
11
 
// Implement the v2 Key Pair form of identity based on Keystone
12
 
 
13
 
type KeyPairRequest struct {
14
 
        Auth struct {
15
 
                ApiAccessKeyCredentials struct {
16
 
                        AccessKey string `json:"accessKey"`
17
 
                        SecretKey string `json:"secretKey"`
18
 
                } `json:"apiAccessKeyCredentials"`
19
 
                TenantName string `json:"tenantName"`
20
 
        } `json:"auth"`
21
 
}
22
 
 
23
 
type KeyPair struct {
24
 
        hook.TestService
25
 
        Users
26
 
        services []Service
27
 
}
28
 
 
29
 
func NewKeyPair() *KeyPair {
30
 
        return &KeyPair{
31
 
                Users: Users{
32
 
                        users:   make(map[string]UserInfo),
33
 
                        tenants: make(map[string]string),
34
 
                },
35
 
        }
36
 
}
37
 
 
38
 
func (u *KeyPair) RegisterServiceProvider(name, serviceType string, serviceProvider ServiceProvider) {
39
 
        service := Service{name, serviceType, serviceProvider.Endpoints()}
40
 
        u.AddService(service)
41
 
}
42
 
 
43
 
func (u *KeyPair) AddService(service Service) {
44
 
        u.services = append(u.services, service)
45
 
}
46
 
 
47
 
func (u *KeyPair) ReturnFailure(w http.ResponseWriter, status int, message string) {
48
 
        e := ErrorWrapper{
49
 
                Error: ErrorResponse{
50
 
                        Message: message,
51
 
                        Code:    status,
52
 
                        Title:   http.StatusText(status),
53
 
                },
54
 
        }
55
 
        if content, err := json.Marshal(e); err != nil {
56
 
                w.Header().Set("Content-Length", fmt.Sprintf("%d", len(internalError)))
57
 
                w.WriteHeader(http.StatusInternalServerError)
58
 
                w.Write(internalError)
59
 
        } else {
60
 
                w.Header().Set("Content-Length", fmt.Sprintf("%d", len(content)))
61
 
                w.WriteHeader(status)
62
 
                w.Write(content)
63
 
        }
64
 
}
65
 
 
66
 
func (u *KeyPair) ServeHTTP(w http.ResponseWriter, r *http.Request) {
67
 
        var req KeyPairRequest
68
 
        // Testing against Canonistack, all responses are application/json, even failures
69
 
        w.Header().Set("Content-Type", "application/json")
70
 
        if r.Header.Get("Content-Type") != "application/json" {
71
 
                u.ReturnFailure(w, http.StatusBadRequest, notJSON)
72
 
                return
73
 
        }
74
 
        if content, err := ioutil.ReadAll(r.Body); err != nil {
75
 
                w.WriteHeader(http.StatusBadRequest)
76
 
                return
77
 
        } else {
78
 
                if err := json.Unmarshal(content, &req); err != nil {
79
 
                        u.ReturnFailure(w, http.StatusBadRequest, notJSON)
80
 
                        return
81
 
                }
82
 
        }
83
 
        userInfo, errmsg := u.authenticate(req.Auth.ApiAccessKeyCredentials.AccessKey, req.Auth.ApiAccessKeyCredentials.SecretKey)
84
 
        if errmsg != "" {
85
 
                u.ReturnFailure(w, http.StatusUnauthorized, errmsg)
86
 
                return
87
 
        }
88
 
        res, err := u.generateAccessResponse(userInfo)
89
 
        if err != nil {
90
 
                u.ReturnFailure(w, http.StatusInternalServerError, err.Error())
91
 
                return
92
 
        }
93
 
        content, err := json.Marshal(res)
94
 
        if err != nil {
95
 
                u.ReturnFailure(w, http.StatusInternalServerError, err.Error())
96
 
                return
97
 
        }
98
 
        w.WriteHeader(http.StatusOK)
99
 
        w.Write(content)
100
 
}
101
 
 
102
 
func (u *KeyPair) generateAccessResponse(userInfo *UserInfo) (*AccessResponse, error) {
103
 
        res := AccessResponse{}
104
 
        // We pre-populate the response with genuine entries so that it looks sane.
105
 
        if err := json.Unmarshal([]byte(exampleResponse), &res); err != nil {
106
 
                return nil, err
107
 
        }
108
 
        res.Access.ServiceCatalog = u.services
109
 
        res.Access.Token.Id = userInfo.Token
110
 
        res.Access.Token.Tenant.Id = userInfo.TenantId
111
 
        res.Access.User.Id = userInfo.Id
112
 
        if err := u.ProcessControlHook("authorisation", u, &res, userInfo); err != nil {
113
 
                return nil, err
114
 
        }
115
 
        return &res, nil
116
 
}
117
 
 
118
 
// setupHTTP attaches all the needed handlers to provide the HTTP API.
119
 
func (u *KeyPair) SetupHTTP(mux *http.ServeMux) {
120
 
        mux.Handle("/tokens", u)
121
 
}