/
api_Is_authed.go
47 lines (43 loc) · 1.4 KB
/
api_Is_authed.go
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
package grpcserver
import (
"context"
"net/http"
"github.com/KonstantinGasser/datalab/common"
"github.com/KonstantinGasser/datalab/service.user.auth.agent/cmd/grpcserver/proto"
"github.com/sirupsen/logrus"
)
func (server UserAuthServer) IsAuthed(ctx context.Context, in *proto.IsAuthedRequest) (*proto.IsAuthedResponse, error) {
tracingId := in.GetTracing_ID()
logrus.Infof("[%v][server.IsAuthed] received request\n", tracingId)
authedUser, err := server.authService.Authenticate(ctx, in.GetAccessToken())
if err != nil {
logrus.Errorf("[%v][server.IsAuthed] could not autenticate user: %v\n", tracingId, err.Error())
return &proto.IsAuthedResponse{
StatusCode: err.Code(),
Msg: err.Info(),
IsAuthed: false,
AuthedUser: nil,
}, nil
}
permisions, err := server.fetchService.GetById(ctx, authedUser.Uuid)
if err != nil {
logrus.Errorf("[%v][server.IsAuthed] could not get permissions of user: %v\n", tracingId, err.Error())
return &proto.IsAuthedResponse{
StatusCode: err.Code(),
Msg: err.Info(),
IsAuthed: false,
AuthedUser: nil,
}, nil
}
return &proto.IsAuthedResponse{
StatusCode: http.StatusOK,
Msg: "User logged in",
IsAuthed: true,
AuthedUser: &common.AuthedUser{
Uuid: authedUser.Uuid,
Username: authedUser.Username,
Organization: authedUser.Organization,
ReadWriteApps: permisions.AllowedApps(),
},
}, nil
}