mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-20 06:25:07 +00:00
add users service
This commit is contained in:
3
users/Dockerfile
Normal file
3
users/Dockerfile
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
FROM alpine:3.2
|
||||||
|
ADD users /users
|
||||||
|
ENTRYPOINT [ "/users" ]
|
||||||
22
users/Makefile
Normal file
22
users/Makefile
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
|
||||||
|
GOPATH:=$(shell go env GOPATH)
|
||||||
|
.PHONY: init
|
||||||
|
init:
|
||||||
|
go get -u github.com/golang/protobuf/proto
|
||||||
|
go get -u github.com/golang/protobuf/protoc-gen-go
|
||||||
|
go get github.com/micro/micro/v3/cmd/protoc-gen-micro
|
||||||
|
.PHONY: proto
|
||||||
|
proto:
|
||||||
|
protoc --proto_path=. --micro_out=. --go_out=:. proto/users.proto
|
||||||
|
|
||||||
|
.PHONY: build
|
||||||
|
build:
|
||||||
|
go build -o users *.go
|
||||||
|
|
||||||
|
.PHONY: test
|
||||||
|
test:
|
||||||
|
go test -v ./... -cover
|
||||||
|
|
||||||
|
.PHONY: docker
|
||||||
|
docker:
|
||||||
|
docker build . -t users:latest
|
||||||
6
users/README.md
Normal file
6
users/README.md
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
User management and authentication
|
||||||
|
|
||||||
|
# Users Service
|
||||||
|
|
||||||
|
The users service provides user management and authentication
|
||||||
|
|
||||||
150
users/domain/domain.go
Normal file
150
users/domain/domain.go
Normal file
@@ -0,0 +1,150 @@
|
|||||||
|
package domain
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
user "github.com/micro/services/users/proto"
|
||||||
|
"github.com/micro/micro/v3/service/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
type pw struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
Salt string `json:"salt"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Domain struct {
|
||||||
|
users model.Model
|
||||||
|
sessions model.Model
|
||||||
|
passwords model.Model
|
||||||
|
|
||||||
|
nameIndex model.Index
|
||||||
|
emailIndex model.Index
|
||||||
|
idIndex model.Index
|
||||||
|
}
|
||||||
|
|
||||||
|
func New() *Domain {
|
||||||
|
nameIndex := model.ByEquality("username")
|
||||||
|
nameIndex.Unique = true
|
||||||
|
nameIndex.Order.Type = model.OrderTypeUnordered
|
||||||
|
|
||||||
|
emailIndex := model.ByEquality("email")
|
||||||
|
emailIndex.Unique = true
|
||||||
|
emailIndex.Order.Type = model.OrderTypeUnordered
|
||||||
|
|
||||||
|
// @todo there should be a better way to get the default index from model
|
||||||
|
// than recreating the options here
|
||||||
|
idIndex := model.ByEquality("id")
|
||||||
|
idIndex.Order.Type = model.OrderTypeUnordered
|
||||||
|
|
||||||
|
return &Domain{
|
||||||
|
users: model.New(user.User{}, &model.Options{
|
||||||
|
Indexes: []model.Index{nameIndex, emailIndex},
|
||||||
|
}),
|
||||||
|
sessions: model.New(user.Session{}, nil),
|
||||||
|
passwords: model.New(pw{}, nil),
|
||||||
|
nameIndex: nameIndex,
|
||||||
|
emailIndex: emailIndex,
|
||||||
|
idIndex: idIndex,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (domain *Domain) CreateSession(sess *user.Session) error {
|
||||||
|
if sess.Created == 0 {
|
||||||
|
sess.Created = time.Now().Unix()
|
||||||
|
}
|
||||||
|
|
||||||
|
if sess.Expires == 0 {
|
||||||
|
sess.Expires = time.Now().Add(time.Hour * 24 * 7).Unix()
|
||||||
|
}
|
||||||
|
|
||||||
|
return domain.sessions.Create(sess)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (domain *Domain) DeleteSession(id string) error {
|
||||||
|
return domain.sessions.Delete(domain.idIndex.ToQuery(id))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (domain *Domain) ReadSession(id string) (*user.Session, error) {
|
||||||
|
sess := &user.Session{}
|
||||||
|
// @todo there should be a Read in the model to get rid of this pattern
|
||||||
|
return sess, domain.sessions.Read(domain.idIndex.ToQuery(id), &sess)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (domain *Domain) Create(user *user.User, salt string, password string) error {
|
||||||
|
user.Created = time.Now().Unix()
|
||||||
|
user.Updated = time.Now().Unix()
|
||||||
|
err := domain.users.Create(user)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return domain.passwords.Create(pw{
|
||||||
|
ID: user.Id,
|
||||||
|
Password: password,
|
||||||
|
Salt: salt,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (domain *Domain) Delete(id string) error {
|
||||||
|
return domain.users.Delete(domain.idIndex.ToQuery(id))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (domain *Domain) Update(user *user.User) error {
|
||||||
|
user.Updated = time.Now().Unix()
|
||||||
|
return domain.users.Create(user)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (domain *Domain) Read(id string) (*user.User, error) {
|
||||||
|
user := &user.User{}
|
||||||
|
return user, domain.users.Read(domain.idIndex.ToQuery(id), user)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (domain *Domain) Search(username, email string, limit, offset int64) ([]*user.User, error) {
|
||||||
|
var query model.Query
|
||||||
|
if len(username) > 0 {
|
||||||
|
query = domain.nameIndex.ToQuery(username)
|
||||||
|
} else if len(email) > 0 {
|
||||||
|
query = domain.emailIndex.ToQuery(email)
|
||||||
|
} else {
|
||||||
|
return nil, errors.New("username and email cannot be blank")
|
||||||
|
}
|
||||||
|
|
||||||
|
users := []*user.User{}
|
||||||
|
return users, domain.users.Read(query, &users)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (domain *Domain) UpdatePassword(id string, salt string, password string) error {
|
||||||
|
return domain.passwords.Create(pw{
|
||||||
|
ID: id,
|
||||||
|
Password: password,
|
||||||
|
Salt: salt,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (domain *Domain) SaltAndPassword(username, email string) (string, string, error) {
|
||||||
|
var query model.Query
|
||||||
|
if len(username) > 0 {
|
||||||
|
query = domain.nameIndex.ToQuery(username)
|
||||||
|
} else if len(email) > 0 {
|
||||||
|
query = domain.emailIndex.ToQuery(email)
|
||||||
|
} else {
|
||||||
|
return "", "", errors.New("username and email cannot be blank")
|
||||||
|
}
|
||||||
|
|
||||||
|
user := &user.User{}
|
||||||
|
err := domain.users.Read(query, &user)
|
||||||
|
if err != nil {
|
||||||
|
return "", "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
query = model.QueryEquals("id", user.Id)
|
||||||
|
query.Order.Type = model.OrderTypeUnordered
|
||||||
|
|
||||||
|
password := &pw{}
|
||||||
|
err = domain.passwords.Read(query, password)
|
||||||
|
if err != nil {
|
||||||
|
return "", "", err
|
||||||
|
}
|
||||||
|
return password.Salt, password.Password, nil
|
||||||
|
}
|
||||||
3
users/generate.go
Normal file
3
users/generate.go
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
//go:generate make proto
|
||||||
174
users/handler/handler.go
Normal file
174
users/handler/handler.go
Normal file
@@ -0,0 +1,174 @@
|
|||||||
|
package handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"encoding/base64"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/micro/services/users/domain"
|
||||||
|
pb "github.com/micro/services/users/proto"
|
||||||
|
"github.com/micro/micro/v3/service/errors"
|
||||||
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
x = "cruft123"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
alphanum = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
|
||||||
|
)
|
||||||
|
|
||||||
|
func random(i int) string {
|
||||||
|
bytes := make([]byte, i)
|
||||||
|
for {
|
||||||
|
rand.Read(bytes)
|
||||||
|
for i, b := range bytes {
|
||||||
|
bytes[i] = alphanum[b%byte(len(alphanum))]
|
||||||
|
}
|
||||||
|
return string(bytes)
|
||||||
|
}
|
||||||
|
return "ughwhy?!!!"
|
||||||
|
}
|
||||||
|
|
||||||
|
type Users struct {
|
||||||
|
domain *domain.Domain
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewUsers() *Users {
|
||||||
|
return &Users{
|
||||||
|
domain: domain.New(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Users) Create(ctx context.Context, req *pb.CreateRequest, rsp *pb.CreateResponse) error {
|
||||||
|
if len(req.Password) < 8 {
|
||||||
|
return errors.InternalServerError("users.Create.Check", "Password is less than 8 characters")
|
||||||
|
}
|
||||||
|
salt := random(16)
|
||||||
|
h, err := bcrypt.GenerateFromPassword([]byte(x+salt+req.Password), 10)
|
||||||
|
if err != nil {
|
||||||
|
return errors.InternalServerError("users.Create", err.Error())
|
||||||
|
}
|
||||||
|
pp := base64.StdEncoding.EncodeToString(h)
|
||||||
|
|
||||||
|
return s.domain.Create(&pb.User{
|
||||||
|
Id: req.Id,
|
||||||
|
Username: strings.ToLower(req.Username),
|
||||||
|
Email: strings.ToLower(req.Email),
|
||||||
|
}, salt, pp)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Users) Read(ctx context.Context, req *pb.ReadRequest, rsp *pb.ReadResponse) error {
|
||||||
|
user, err := s.domain.Read(req.Id)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
rsp.User = user
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Users) Update(ctx context.Context, req *pb.UpdateRequest, rsp *pb.UpdateResponse) error {
|
||||||
|
return s.domain.Update(&pb.User{
|
||||||
|
Id: req.Id,
|
||||||
|
Username: strings.ToLower(req.Username),
|
||||||
|
Email: strings.ToLower(req.Email),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Users) Delete(ctx context.Context, req *pb.DeleteRequest, rsp *pb.DeleteResponse) error {
|
||||||
|
return s.domain.Delete(req.Id)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Users) Search(ctx context.Context, req *pb.SearchRequest, rsp *pb.SearchResponse) error {
|
||||||
|
users, err := s.domain.Search(req.Username, req.Email, req.Limit, req.Offset)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
rsp.Users = users
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Users) UpdatePassword(ctx context.Context, req *pb.UpdatePasswordRequest, rsp *pb.UpdatePasswordResponse) error {
|
||||||
|
usr, err := s.domain.Read(req.UserId)
|
||||||
|
if err != nil {
|
||||||
|
return errors.InternalServerError("users.updatepassword", err.Error())
|
||||||
|
}
|
||||||
|
if req.NewPassword != req.ConfirmPassword {
|
||||||
|
return errors.InternalServerError("users.updatepassword", "Passwords don't math")
|
||||||
|
}
|
||||||
|
|
||||||
|
salt, hashed, err := s.domain.SaltAndPassword(usr.Username, usr.Email)
|
||||||
|
if err != nil {
|
||||||
|
return errors.InternalServerError("users.updatepassword", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
hh, err := base64.StdEncoding.DecodeString(hashed)
|
||||||
|
if err != nil {
|
||||||
|
return errors.InternalServerError("users.updatepassword", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := bcrypt.CompareHashAndPassword(hh, []byte(x+salt+req.OldPassword)); err != nil {
|
||||||
|
return errors.Unauthorized("users.updatepassword", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
salt = random(16)
|
||||||
|
h, err := bcrypt.GenerateFromPassword([]byte(x+salt+req.NewPassword), 10)
|
||||||
|
if err != nil {
|
||||||
|
return errors.InternalServerError("users.updatepassword", err.Error())
|
||||||
|
}
|
||||||
|
pp := base64.StdEncoding.EncodeToString(h)
|
||||||
|
|
||||||
|
if err := s.domain.UpdatePassword(req.UserId, salt, pp); err != nil {
|
||||||
|
return errors.InternalServerError("users.updatepassword", err.Error())
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Users) Login(ctx context.Context, req *pb.LoginRequest, rsp *pb.LoginResponse) error {
|
||||||
|
username := strings.ToLower(req.Username)
|
||||||
|
email := strings.ToLower(req.Email)
|
||||||
|
|
||||||
|
salt, hashed, err := s.domain.SaltAndPassword(username, email)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
hh, err := base64.StdEncoding.DecodeString(hashed)
|
||||||
|
if err != nil {
|
||||||
|
return errors.InternalServerError("users.Login", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := bcrypt.CompareHashAndPassword(hh, []byte(x+salt+req.Password)); err != nil {
|
||||||
|
return errors.Unauthorized("users.login", err.Error())
|
||||||
|
}
|
||||||
|
// save session
|
||||||
|
sess := &pb.Session{
|
||||||
|
Id: random(128),
|
||||||
|
Username: username,
|
||||||
|
Email: email,
|
||||||
|
Created: time.Now().Unix(),
|
||||||
|
Expires: time.Now().Add(time.Hour * 24 * 7).Unix(),
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.domain.CreateSession(sess); err != nil {
|
||||||
|
return errors.InternalServerError("users.Login", err.Error())
|
||||||
|
}
|
||||||
|
rsp.Session = sess
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Users) Logout(ctx context.Context, req *pb.LogoutRequest, rsp *pb.LogoutResponse) error {
|
||||||
|
return s.domain.DeleteSession(req.SessionId)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Users) ReadSession(ctx context.Context, req *pb.ReadSessionRequest, rsp *pb.ReadSessionResponse) error {
|
||||||
|
sess, err := s.domain.ReadSession(req.SessionId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
rsp.Session = sess
|
||||||
|
return nil
|
||||||
|
}
|
||||||
22
users/main.go
Normal file
22
users/main.go
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/micro/services/users/handler"
|
||||||
|
proto "github.com/micro/services/users/proto"
|
||||||
|
"github.com/micro/micro/v3/service"
|
||||||
|
"github.com/micro/micro/v3/service/logger"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
service := service.New(
|
||||||
|
service.Name("users"),
|
||||||
|
)
|
||||||
|
|
||||||
|
service.Init()
|
||||||
|
|
||||||
|
proto.RegisterUsersHandler(service.Server(), handler.NewUsers())
|
||||||
|
|
||||||
|
if err := service.Run(); err != nil {
|
||||||
|
logger.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
1536
users/proto/users.pb.go
Normal file
1536
users/proto/users.pb.go
Normal file
File diff suppressed because it is too large
Load Diff
229
users/proto/users.pb.micro.go
Normal file
229
users/proto/users.pb.micro.go
Normal file
@@ -0,0 +1,229 @@
|
|||||||
|
// Code generated by protoc-gen-micro. DO NOT EDIT.
|
||||||
|
// source: proto/users.proto
|
||||||
|
|
||||||
|
package users
|
||||||
|
|
||||||
|
import (
|
||||||
|
fmt "fmt"
|
||||||
|
proto "github.com/golang/protobuf/proto"
|
||||||
|
math "math"
|
||||||
|
)
|
||||||
|
|
||||||
|
import (
|
||||||
|
context "context"
|
||||||
|
api "github.com/micro/micro/v3/service/api"
|
||||||
|
client "github.com/micro/micro/v3/service/client"
|
||||||
|
server "github.com/micro/micro/v3/service/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Reference imports to suppress errors if they are not otherwise used.
|
||||||
|
var _ = proto.Marshal
|
||||||
|
var _ = fmt.Errorf
|
||||||
|
var _ = math.Inf
|
||||||
|
|
||||||
|
// This is a compile-time assertion to ensure that this generated file
|
||||||
|
// is compatible with the proto package it is being compiled against.
|
||||||
|
// A compilation error at this line likely means your copy of the
|
||||||
|
// proto package needs to be updated.
|
||||||
|
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||||
|
|
||||||
|
// Reference imports to suppress errors if they are not otherwise used.
|
||||||
|
var _ api.Endpoint
|
||||||
|
var _ context.Context
|
||||||
|
var _ client.Option
|
||||||
|
var _ server.Option
|
||||||
|
|
||||||
|
// Api Endpoints for Users service
|
||||||
|
|
||||||
|
func NewUsersEndpoints() []*api.Endpoint {
|
||||||
|
return []*api.Endpoint{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Client API for Users service
|
||||||
|
|
||||||
|
type UsersService interface {
|
||||||
|
Create(ctx context.Context, in *CreateRequest, opts ...client.CallOption) (*CreateResponse, error)
|
||||||
|
Read(ctx context.Context, in *ReadRequest, opts ...client.CallOption) (*ReadResponse, error)
|
||||||
|
Update(ctx context.Context, in *UpdateRequest, opts ...client.CallOption) (*UpdateResponse, error)
|
||||||
|
Delete(ctx context.Context, in *DeleteRequest, opts ...client.CallOption) (*DeleteResponse, error)
|
||||||
|
Search(ctx context.Context, in *SearchRequest, opts ...client.CallOption) (*SearchResponse, error)
|
||||||
|
UpdatePassword(ctx context.Context, in *UpdatePasswordRequest, opts ...client.CallOption) (*UpdatePasswordResponse, error)
|
||||||
|
Login(ctx context.Context, in *LoginRequest, opts ...client.CallOption) (*LoginResponse, error)
|
||||||
|
Logout(ctx context.Context, in *LogoutRequest, opts ...client.CallOption) (*LogoutResponse, error)
|
||||||
|
ReadSession(ctx context.Context, in *ReadSessionRequest, opts ...client.CallOption) (*ReadSessionResponse, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type usersService struct {
|
||||||
|
c client.Client
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewUsersService(name string, c client.Client) UsersService {
|
||||||
|
return &usersService{
|
||||||
|
c: c,
|
||||||
|
name: name,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *usersService) Create(ctx context.Context, in *CreateRequest, opts ...client.CallOption) (*CreateResponse, error) {
|
||||||
|
req := c.c.NewRequest(c.name, "Users.Create", in)
|
||||||
|
out := new(CreateResponse)
|
||||||
|
err := c.c.Call(ctx, req, out, opts...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *usersService) Read(ctx context.Context, in *ReadRequest, opts ...client.CallOption) (*ReadResponse, error) {
|
||||||
|
req := c.c.NewRequest(c.name, "Users.Read", in)
|
||||||
|
out := new(ReadResponse)
|
||||||
|
err := c.c.Call(ctx, req, out, opts...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *usersService) Update(ctx context.Context, in *UpdateRequest, opts ...client.CallOption) (*UpdateResponse, error) {
|
||||||
|
req := c.c.NewRequest(c.name, "Users.Update", in)
|
||||||
|
out := new(UpdateResponse)
|
||||||
|
err := c.c.Call(ctx, req, out, opts...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *usersService) Delete(ctx context.Context, in *DeleteRequest, opts ...client.CallOption) (*DeleteResponse, error) {
|
||||||
|
req := c.c.NewRequest(c.name, "Users.Delete", in)
|
||||||
|
out := new(DeleteResponse)
|
||||||
|
err := c.c.Call(ctx, req, out, opts...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *usersService) Search(ctx context.Context, in *SearchRequest, opts ...client.CallOption) (*SearchResponse, error) {
|
||||||
|
req := c.c.NewRequest(c.name, "Users.Search", in)
|
||||||
|
out := new(SearchResponse)
|
||||||
|
err := c.c.Call(ctx, req, out, opts...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *usersService) UpdatePassword(ctx context.Context, in *UpdatePasswordRequest, opts ...client.CallOption) (*UpdatePasswordResponse, error) {
|
||||||
|
req := c.c.NewRequest(c.name, "Users.UpdatePassword", in)
|
||||||
|
out := new(UpdatePasswordResponse)
|
||||||
|
err := c.c.Call(ctx, req, out, opts...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *usersService) Login(ctx context.Context, in *LoginRequest, opts ...client.CallOption) (*LoginResponse, error) {
|
||||||
|
req := c.c.NewRequest(c.name, "Users.Login", in)
|
||||||
|
out := new(LoginResponse)
|
||||||
|
err := c.c.Call(ctx, req, out, opts...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *usersService) Logout(ctx context.Context, in *LogoutRequest, opts ...client.CallOption) (*LogoutResponse, error) {
|
||||||
|
req := c.c.NewRequest(c.name, "Users.Logout", in)
|
||||||
|
out := new(LogoutResponse)
|
||||||
|
err := c.c.Call(ctx, req, out, opts...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *usersService) ReadSession(ctx context.Context, in *ReadSessionRequest, opts ...client.CallOption) (*ReadSessionResponse, error) {
|
||||||
|
req := c.c.NewRequest(c.name, "Users.ReadSession", in)
|
||||||
|
out := new(ReadSessionResponse)
|
||||||
|
err := c.c.Call(ctx, req, out, opts...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Server API for Users service
|
||||||
|
|
||||||
|
type UsersHandler interface {
|
||||||
|
Create(context.Context, *CreateRequest, *CreateResponse) error
|
||||||
|
Read(context.Context, *ReadRequest, *ReadResponse) error
|
||||||
|
Update(context.Context, *UpdateRequest, *UpdateResponse) error
|
||||||
|
Delete(context.Context, *DeleteRequest, *DeleteResponse) error
|
||||||
|
Search(context.Context, *SearchRequest, *SearchResponse) error
|
||||||
|
UpdatePassword(context.Context, *UpdatePasswordRequest, *UpdatePasswordResponse) error
|
||||||
|
Login(context.Context, *LoginRequest, *LoginResponse) error
|
||||||
|
Logout(context.Context, *LogoutRequest, *LogoutResponse) error
|
||||||
|
ReadSession(context.Context, *ReadSessionRequest, *ReadSessionResponse) error
|
||||||
|
}
|
||||||
|
|
||||||
|
func RegisterUsersHandler(s server.Server, hdlr UsersHandler, opts ...server.HandlerOption) error {
|
||||||
|
type users interface {
|
||||||
|
Create(ctx context.Context, in *CreateRequest, out *CreateResponse) error
|
||||||
|
Read(ctx context.Context, in *ReadRequest, out *ReadResponse) error
|
||||||
|
Update(ctx context.Context, in *UpdateRequest, out *UpdateResponse) error
|
||||||
|
Delete(ctx context.Context, in *DeleteRequest, out *DeleteResponse) error
|
||||||
|
Search(ctx context.Context, in *SearchRequest, out *SearchResponse) error
|
||||||
|
UpdatePassword(ctx context.Context, in *UpdatePasswordRequest, out *UpdatePasswordResponse) error
|
||||||
|
Login(ctx context.Context, in *LoginRequest, out *LoginResponse) error
|
||||||
|
Logout(ctx context.Context, in *LogoutRequest, out *LogoutResponse) error
|
||||||
|
ReadSession(ctx context.Context, in *ReadSessionRequest, out *ReadSessionResponse) error
|
||||||
|
}
|
||||||
|
type Users struct {
|
||||||
|
users
|
||||||
|
}
|
||||||
|
h := &usersHandler{hdlr}
|
||||||
|
return s.Handle(s.NewHandler(&Users{h}, opts...))
|
||||||
|
}
|
||||||
|
|
||||||
|
type usersHandler struct {
|
||||||
|
UsersHandler
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *usersHandler) Create(ctx context.Context, in *CreateRequest, out *CreateResponse) error {
|
||||||
|
return h.UsersHandler.Create(ctx, in, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *usersHandler) Read(ctx context.Context, in *ReadRequest, out *ReadResponse) error {
|
||||||
|
return h.UsersHandler.Read(ctx, in, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *usersHandler) Update(ctx context.Context, in *UpdateRequest, out *UpdateResponse) error {
|
||||||
|
return h.UsersHandler.Update(ctx, in, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *usersHandler) Delete(ctx context.Context, in *DeleteRequest, out *DeleteResponse) error {
|
||||||
|
return h.UsersHandler.Delete(ctx, in, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *usersHandler) Search(ctx context.Context, in *SearchRequest, out *SearchResponse) error {
|
||||||
|
return h.UsersHandler.Search(ctx, in, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *usersHandler) UpdatePassword(ctx context.Context, in *UpdatePasswordRequest, out *UpdatePasswordResponse) error {
|
||||||
|
return h.UsersHandler.UpdatePassword(ctx, in, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *usersHandler) Login(ctx context.Context, in *LoginRequest, out *LoginResponse) error {
|
||||||
|
return h.UsersHandler.Login(ctx, in, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *usersHandler) Logout(ctx context.Context, in *LogoutRequest, out *LogoutResponse) error {
|
||||||
|
return h.UsersHandler.Logout(ctx, in, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *usersHandler) ReadSession(ctx context.Context, in *ReadSessionRequest, out *ReadSessionResponse) error {
|
||||||
|
return h.UsersHandler.ReadSession(ctx, in, out)
|
||||||
|
}
|
||||||
110
users/proto/users.proto
Normal file
110
users/proto/users.proto
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
syntax = "proto3";
|
||||||
|
|
||||||
|
service Users {
|
||||||
|
rpc Create(CreateRequest) returns (CreateResponse) {}
|
||||||
|
rpc Read(ReadRequest) returns (ReadResponse) {}
|
||||||
|
rpc Update(UpdateRequest) returns (UpdateResponse) {}
|
||||||
|
rpc Delete(DeleteRequest) returns (DeleteResponse) {}
|
||||||
|
rpc Search(SearchRequest) returns (SearchResponse) {}
|
||||||
|
rpc UpdatePassword(UpdatePasswordRequest) returns (UpdatePasswordResponse) {}
|
||||||
|
rpc Login(LoginRequest) returns (LoginResponse) {}
|
||||||
|
rpc Logout(LogoutRequest) returns (LogoutResponse) {}
|
||||||
|
rpc ReadSession(ReadSessionRequest) returns(ReadSessionResponse) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
message User {
|
||||||
|
string id = 1; // uuid
|
||||||
|
string username = 2; // alphanumeric user or org
|
||||||
|
string email = 3;
|
||||||
|
int64 created = 4; // unix
|
||||||
|
int64 updated = 5; // unix
|
||||||
|
}
|
||||||
|
|
||||||
|
message Session {
|
||||||
|
string id = 1;
|
||||||
|
string username = 2;
|
||||||
|
string email = 3;
|
||||||
|
int64 created = 4; // unix
|
||||||
|
int64 expires = 5; // unix
|
||||||
|
}
|
||||||
|
|
||||||
|
message CreateRequest {
|
||||||
|
string id = 1; // uuid
|
||||||
|
string username = 2; // alphanumeric user or org
|
||||||
|
string email = 3;
|
||||||
|
string password = 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
message CreateResponse {
|
||||||
|
}
|
||||||
|
|
||||||
|
message DeleteRequest {
|
||||||
|
string id = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message DeleteResponse {
|
||||||
|
}
|
||||||
|
|
||||||
|
message ReadRequest {
|
||||||
|
string id = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message ReadResponse {
|
||||||
|
User user = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message UpdateRequest {
|
||||||
|
string id = 1; // uuid
|
||||||
|
string username = 2; // alphanumeric user or org
|
||||||
|
string email = 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
message UpdateResponse {
|
||||||
|
}
|
||||||
|
|
||||||
|
message UpdatePasswordRequest {
|
||||||
|
string userId = 1;
|
||||||
|
string oldPassword = 2;
|
||||||
|
string newPassword = 3;
|
||||||
|
string confirm_password = 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
message UpdatePasswordResponse {
|
||||||
|
}
|
||||||
|
|
||||||
|
message SearchRequest {
|
||||||
|
string username = 1;
|
||||||
|
string email = 2;
|
||||||
|
int64 limit = 3;
|
||||||
|
int64 offset = 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
message SearchResponse {
|
||||||
|
repeated User users = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message ReadSessionRequest {
|
||||||
|
string sessionId = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message ReadSessionResponse {
|
||||||
|
Session session = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message LoginRequest {
|
||||||
|
string username = 1;
|
||||||
|
string email = 2;
|
||||||
|
string password = 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
message LoginResponse {
|
||||||
|
Session session = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message LogoutRequest {
|
||||||
|
string sessionId = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message LogoutResponse {
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user