-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthService.go
More file actions
54 lines (46 loc) · 1.42 KB
/
Copy pathauthService.go
File metadata and controls
54 lines (46 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"database/sql"
"fmt"
"go_with_sql/iam/auth"
"go_with_sql/repository"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"log"
"net"
)
func gRPCServer() {
listen, err := net.Listen("tcp", ":9090")
checkError(err)
grpcServer := grpc.NewServer()
auth.RegisterDefaultAuthenticationServiceServer(grpcServer, &server{})
log.Println("Server listening on port :9090")
if err := grpcServer.Serve(listen); err != nil {
checkError(err)
}
}
type server struct {
auth.UnimplementedDefaultAuthenticationServiceServer
}
func (s *server) RequestPasswordReset(ctx context.Context, req *auth.UserReq) (*auth.StandardResponse, error) {
// Implementation
return nil, status.Error(codes.Unimplemented, "not implemented")
}
func (s *server) Authenticate(ctx context.Context, req *auth.AuthReq) (*auth.StandardResponse, error) {
fmt.Printf(">> AuthReq > %s\n", req)
connection, err := repository.GetDbConnection()
checkError(err)
defer func(connection *sql.DB) {
err := connection.Close()
repository.CheckError(err)
}(connection)
user := repository.GetUserByUsername(connection, req.Username)
validated := repository.ValidatePassword(user.Password, req.Password)
if validated {
return &auth.StandardResponse{Code: 200, Message: "<<Login successful"}, nil
} else {
return &auth.StandardResponse{Code: 400, Message: "<<Invalid Credentials"}, nil
}
}