Christopher James Hayward
1 year ago
2 changed files with 0 additions and 205 deletions
@ -1,69 +0,0 @@ |
|||
package main |
|||
|
|||
import ( |
|||
"flag" |
|||
"fmt" |
|||
"log" |
|||
"net/http" |
|||
"os" |
|||
|
|||
"git.chrishayward.xyz/x/users/gateway" |
|||
"git.chrishayward.xyz/x/users/proto" |
|||
"google.golang.org/grpc" |
|||
"gopkg.in/yaml.v3" |
|||
) |
|||
|
|||
var configPath = flag.String("config", "./config/gateway.yaml", "--config=./config/gateway.yaml") |
|||
|
|||
type config struct { |
|||
Port int `yaml:"port"` |
|||
Domain string `yaml:"domain"` |
|||
Server struct { |
|||
Address string `yaml:"address"` |
|||
Port int `yaml:"port"` |
|||
Secret string `yaml:"secret"` |
|||
} `yaml:"server"` |
|||
} |
|||
|
|||
func main() { |
|||
// Parse the optional flags.
|
|||
flag.Parse() |
|||
|
|||
// Read the config file.
|
|||
config := &config{} |
|||
file, err := os.Open(*configPath) |
|||
if err != nil { |
|||
log.Fatal(err) |
|||
} |
|||
defer file.Close() |
|||
if err := yaml.NewDecoder(file).Decode(&config); err != nil { |
|||
log.Fatal(err) |
|||
} |
|||
|
|||
// Create the connection to the server.
|
|||
conn, err := grpc.Dial( |
|||
fmt.Sprintf("%s:%d", config.Server.Address, config.Server.Port), |
|||
grpc.WithInsecure()) |
|||
if err != nil { |
|||
log.Fatal(err) |
|||
} |
|||
|
|||
// Defer closing the connection.
|
|||
defer conn.Close() |
|||
|
|||
// Create the client.
|
|||
client := proto.NewUsersClient(conn) |
|||
|
|||
// Setup HTTP endpoints.
|
|||
http.HandleFunc("/register", gateway.Register(client)) |
|||
http.HandleFunc("/login", gateway.Login(client)) |
|||
http.HandleFunc("/logout", gateway.Authorize(client, config.Server.Secret, gateway.Logout(client))) |
|||
http.HandleFunc("/reset_password", gateway.ResetPassword(client, fmt.Sprintf("%s, %d", config.Domain, config.Port))) |
|||
http.HandleFunc("/change_password", gateway.ChangePassword(client)) |
|||
|
|||
// Listen for requests.
|
|||
log.Printf("Forwarding from :%d to %s:%d", config.Port, config.Server.Address, config.Server.Port) |
|||
if err := http.ListenAndServe(fmt.Sprintf(":%d", config.Port), nil); err != nil { |
|||
log.Fatal(err) |
|||
} |
|||
} |
@ -1,136 +0,0 @@ |
|||
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) |
|||
}) |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue