You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
136 lines
3.5 KiB
136 lines
3.5 KiB
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"git.chrishayward.xyz/x/users/proto"
|
|
)
|
|
|
|
func Register(client proto.UsersClient) http.HandlerFunc {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
var req proto.RegisterRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
w.Write([]byte(err.Error()))
|
|
return
|
|
}
|
|
|
|
if _, err := client.Register(r.Context(), &req); err != nil {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
w.Write([]byte(err.Error()))
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
})
|
|
}
|
|
|
|
func Login(client proto.UsersClient) http.HandlerFunc {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
var req proto.LoginRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
w.Write([]byte(err.Error()))
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
res, err := client.Login(r.Context(), &req)
|
|
if err != nil {
|
|
w.Write([]byte(err.Error()))
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
w.Write([]byte(res.Token.Token))
|
|
w.WriteHeader(http.StatusOK)
|
|
})
|
|
}
|
|
|
|
func Logout(client proto.UsersClient) http.HandlerFunc {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if _, err := client.Logout(r.Context(), &proto.LogoutRequest{
|
|
Token: r.Context().Value("token").(*proto.UserToken),
|
|
}); err != nil {
|
|
w.Write([]byte(err.Error()))
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
})
|
|
}
|
|
|
|
func Authorize(client proto.UsersClient, secret string, next http.HandlerFunc) http.HandlerFunc {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
var req proto.AuthorizeRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
w.Write([]byte(err.Error()))
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
res, err := client.Authorize(r.Context(), &proto.AuthorizeRequest{
|
|
Secret: secret,
|
|
Token: req.Token,
|
|
})
|
|
|
|
if err != nil {
|
|
w.Write([]byte(err.Error()))
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
ctx := context.WithValue(r.Context(), "user", res.User)
|
|
ctx = context.WithValue(ctx, "roles", res.Roles)
|
|
ctx = context.WithValue(ctx, "token", &proto.UserToken{
|
|
Token: req.Token.Token,
|
|
})
|
|
|
|
next(w, r.WithContext(ctx))
|
|
})
|
|
}
|
|
|
|
func ResetPassword(client proto.UsersClient, endpoint string) http.HandlerFunc {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
var req proto.ResetPasswordRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
w.Write([]byte(err.Error()))
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
res, err := client.ResetPassword(r.Context(), &req)
|
|
if err != nil {
|
|
w.Write([]byte(err.Error()))
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
w.Write([]byte(fmt.Sprintf(
|
|
"Please follow this link to update your password: %s/change_password?token=%s\n",
|
|
endpoint, res.Token.Token)))
|
|
w.WriteHeader(http.StatusOK)
|
|
})
|
|
}
|
|
|
|
func ChangePassword(client proto.UsersClient) http.HandlerFunc {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
var req proto.ChangePasswordRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
w.Write([]byte(err.Error()))
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
if _, err := client.ChangePassword(r.Context(), &req); err != nil {
|
|
w.Write([]byte(err.Error()))
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
})
|
|
}
|