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"
|
2017-02-14 21:14:24 +00:00
|
|
|
"github.com/influxdata/chronograf/oauth2"
|
2016-11-17 23:57:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2017-02-15 18:57:52 +00:00
|
|
|
// If new user response is nil, return an empty userResponse because it
|
|
|
|
// indicates authentication is not needed
|
2016-11-17 23:57:46 +00:00
|
|
|
func newUserResponse(usr *chronograf.User) userResponse {
|
|
|
|
base := "/chronograf/v1/users"
|
2017-02-24 05:26:09 +00:00
|
|
|
name := "me"
|
2017-02-15 18:57:52 +00:00
|
|
|
if usr != nil {
|
2017-02-24 05:26:09 +00:00
|
|
|
// TODO: Change to usrl.PathEscape for go 1.8
|
|
|
|
u := &url.URL{Path: usr.Name}
|
|
|
|
name = u.String()
|
2016-11-17 23:57:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return userResponse{
|
|
|
|
User: usr,
|
|
|
|
Links: userLinks{
|
2017-02-24 05:26:09 +00:00
|
|
|
Self: fmt.Sprintf("%s/%s", base, name),
|
2016-11-17 23:57:46 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getEmail(ctx context.Context) (string, error) {
|
2017-02-15 22:28:17 +00:00
|
|
|
principal, err := getPrincipal(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if principal.Subject == "" {
|
2016-11-17 23:57:46 +00:00
|
|
|
return "", fmt.Errorf("Token not found")
|
|
|
|
}
|
2017-02-15 22:28:17 +00:00
|
|
|
return principal.Subject, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPrincipal(ctx context.Context) (oauth2.Principal, error) {
|
|
|
|
principal, ok := ctx.Value(oauth2.PrincipalKey).(oauth2.Principal)
|
|
|
|
if !ok {
|
|
|
|
return oauth2.Principal{}, fmt.Errorf("Token not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return principal, nil
|
2016-11-17 23:57:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2017-02-15 18:57:52 +00:00
|
|
|
// If there's no authentication, return an empty user
|
|
|
|
res := newUserResponse(nil)
|
|
|
|
encodeJSON(w, http.StatusOK, res, h.Logger)
|
2016-11-18 00:52:19 +00:00
|
|
|
return
|
|
|
|
}
|
2017-02-24 05:26:09 +00:00
|
|
|
|
2016-11-17 23:57:46 +00:00
|
|
|
email, err := getEmail(ctx)
|
|
|
|
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
|
|
|
|
2017-02-24 05:26:09 +00:00
|
|
|
usr, err := h.UsersStore.Get(ctx, email)
|
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-24 05:26:09 +00:00
|
|
|
Name: email,
|
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)
|
|
|
|
}
|