2017-03-10 19:24:48 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
|
|
|
"github.com/influxdata/chronograf"
|
|
|
|
"github.com/influxdata/chronograf/oauth2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type meLinks struct {
|
|
|
|
Self string `json:"self"` // Self link mapping to this resource
|
|
|
|
}
|
|
|
|
|
|
|
|
type meResponse struct {
|
|
|
|
*chronograf.User
|
|
|
|
Links meLinks `json:"links"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// If new user response is nil, return an empty meResponse because it
|
|
|
|
// indicates authentication is not needed
|
|
|
|
func newMeResponse(usr *chronograf.User) meResponse {
|
|
|
|
base := "/chronograf/v1/users"
|
|
|
|
name := "me"
|
|
|
|
if usr != nil {
|
2017-04-07 20:32:35 +00:00
|
|
|
name = PathEscape(usr.Name)
|
2017-03-10 19:24:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return meResponse{
|
|
|
|
User: usr,
|
|
|
|
Links: meLinks{
|
|
|
|
Self: fmt.Sprintf("%s/%s", base, name),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getEmail(ctx context.Context) (string, error) {
|
|
|
|
principal, err := getPrincipal(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if principal.Subject == "" {
|
|
|
|
return "", fmt.Errorf("Token not found")
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Me does a findOrCreate based on the email in the context
|
2017-10-10 22:27:58 +00:00
|
|
|
func (s *Service) Me(w http.ResponseWriter, r *http.Request) {
|
2017-03-10 19:24:48 +00:00
|
|
|
ctx := r.Context()
|
2017-10-10 22:27:58 +00:00
|
|
|
if !s.UseAuth {
|
2017-03-10 19:24:48 +00:00
|
|
|
// If there's no authentication, return an empty user
|
|
|
|
res := newMeResponse(nil)
|
2017-10-10 22:27:58 +00:00
|
|
|
encodeJSON(w, http.StatusOK, res, s.Logger)
|
2017-03-10 19:24:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
email, err := getEmail(ctx)
|
|
|
|
if err != nil {
|
2017-10-10 22:27:58 +00:00
|
|
|
invalidData(w, err, s.Logger)
|
2017-03-10 19:24:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-10 22:27:58 +00:00
|
|
|
usr, err := s.UsersStore.Get(ctx, email)
|
2017-03-10 19:24:48 +00:00
|
|
|
if err == nil {
|
|
|
|
res := newMeResponse(usr)
|
2017-10-10 22:27:58 +00:00
|
|
|
encodeJSON(w, http.StatusOK, res, s.Logger)
|
2017-03-10 19:24:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Because we didnt find a user, making a new one
|
|
|
|
user := &chronograf.User{
|
|
|
|
Name: email,
|
|
|
|
}
|
|
|
|
|
2017-10-10 22:27:58 +00:00
|
|
|
newUser, err := s.UsersStore.Add(ctx, user)
|
2017-03-10 19:24:48 +00:00
|
|
|
if err != nil {
|
|
|
|
msg := fmt.Errorf("error storing user %s: %v", user.Name, err)
|
2017-10-10 22:27:58 +00:00
|
|
|
unknownErrorWithMessage(w, msg, s.Logger)
|
2017-03-10 19:24:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res := newMeResponse(newUser)
|
2017-10-10 22:27:58 +00:00
|
|
|
encodeJSON(w, http.StatusOK, res, s.Logger)
|
2017-03-10 19:24:48 +00:00
|
|
|
}
|