2016-11-17 23:57:46 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2017-02-17 19:37:00 +00:00
|
|
|
"net/url"
|
2016-11-17 23:57:46 +00:00
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
|
|
|
"github.com/influxdata/chronograf"
|
|
|
|
)
|
|
|
|
|
|
|
|
type userLinks struct {
|
2017-02-01 21:05:06 +00:00
|
|
|
Self string `json:"self"` // Self link mapping to this resource
|
2016-11-17 23:57:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type userResponse struct {
|
|
|
|
*chronograf.User
|
|
|
|
Links userLinks `json:"links"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func newUserResponse(usr *chronograf.User) userResponse {
|
|
|
|
base := "/chronograf/v1/users"
|
2017-02-17 19:52:21 +00:00
|
|
|
// TODO: Change to usrl.PathEscape for go 1.8
|
|
|
|
u := &url.URL{Path: usr.Name}
|
|
|
|
encodedUser := u.String()
|
2016-11-17 23:57:46 +00:00
|
|
|
return userResponse{
|
|
|
|
User: usr,
|
|
|
|
Links: userLinks{
|
2017-02-17 20:33:10 +00:00
|
|
|
Self: fmt.Sprintf("%s/%s", base, encodedUser),
|
2016-11-17 23:57:46 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-17 19:37:00 +00:00
|
|
|
func getPrincipal(ctx context.Context) (string, error) {
|
2016-11-17 23:57:46 +00:00
|
|
|
principal := ctx.Value(chronograf.PrincipalKey).(chronograf.Principal)
|
|
|
|
if principal == "" {
|
|
|
|
return "", fmt.Errorf("Token not found")
|
|
|
|
}
|
|
|
|
return string(principal), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Me does a findOrCreate based on the email in the context
|
|
|
|
func (h *Service) Me(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
2016-11-18 00:52:19 +00:00
|
|
|
if !h.UseAuth {
|
2016-12-07 19:28:58 +00:00
|
|
|
// Using status code to signal no need for authentication
|
|
|
|
w.WriteHeader(http.StatusTeapot)
|
2016-11-18 00:52:19 +00:00
|
|
|
return
|
|
|
|
}
|
2017-02-17 19:37:00 +00:00
|
|
|
principal, err := getPrincipal(ctx)
|
2016-11-17 23:57:46 +00:00
|
|
|
if err != nil {
|
2016-11-21 21:24:01 +00:00
|
|
|
invalidData(w, err, h.Logger)
|
2016-11-17 23:57:46 +00:00
|
|
|
return
|
|
|
|
}
|
2017-02-17 19:37:00 +00:00
|
|
|
|
|
|
|
usr, err := h.UsersStore.Get(ctx, principal)
|
2016-11-17 23:57:46 +00:00
|
|
|
if err == nil {
|
|
|
|
res := newUserResponse(usr)
|
|
|
|
encodeJSON(w, http.StatusOK, res, h.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Because we didnt find a user, making a new one
|
|
|
|
user := &chronograf.User{
|
2017-02-17 19:37:00 +00:00
|
|
|
Name: principal,
|
2016-11-17 23:57:46 +00:00
|
|
|
}
|
2017-02-22 03:36:23 +00:00
|
|
|
|
|
|
|
newUser, err := h.UsersStore.Add(ctx, user)
|
2016-11-17 23:57:46 +00:00
|
|
|
if err != nil {
|
2017-02-22 03:36:23 +00:00
|
|
|
msg := fmt.Errorf("error storing user %s: %v", user.Name, err)
|
2016-11-21 21:24:01 +00:00
|
|
|
unknownErrorWithMessage(w, msg, h.Logger)
|
2016-11-17 23:57:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-22 03:36:23 +00:00
|
|
|
res := newUserResponse(newUser)
|
2016-11-17 23:57:46 +00:00
|
|
|
encodeJSON(w, http.StatusOK, res, h.Logger)
|
|
|
|
}
|