Skip to content
This repository was archived by the owner on Feb 4, 2021. It is now read-only.

Commit 45616b4

Browse files
committed
Impl OAuthLogin
1 parent a9035d4 commit 45616b4

File tree

12 files changed

+150
-103
lines changed

12 files changed

+150
-103
lines changed

api/oauth.pb.go

Lines changed: 34 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/oauth.pb.gw.go

Lines changed: 2 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/oauth.swagger.json

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,45 @@
1717
"paths": {
1818
"/oauth/login": {
1919
"get": {
20-
"operationId": "OAuthLogin",
20+
"operationId": "StartOauthLogin",
2121
"responses": {
2222
"200": {
2323
"description": "",
2424
"schema": {
25-
"$ref": "#/definitions/prolab_accountsOAuthLoginResponse"
25+
"$ref": "#/definitions/prolab_accountsStartOAuthLoginResponse"
2626
}
2727
}
2828
},
2929
"parameters": [
3030
{
31-
"name": "name",
32-
"in": "query",
33-
"required": false,
34-
"type": "string"
35-
},
36-
{
37-
"name": "password",
31+
"name": "login_challenge",
3832
"in": "query",
3933
"required": false,
4034
"type": "string"
41-
},
42-
{
43-
"name": "remember",
44-
"in": "query",
45-
"required": false,
46-
"type": "boolean",
47-
"format": "boolean"
48-
},
35+
}
36+
],
37+
"tags": [
38+
"OAuthService"
39+
]
40+
},
41+
"post": {
42+
"operationId": "OAuthLogin",
43+
"responses": {
44+
"200": {
45+
"description": "",
46+
"schema": {
47+
"$ref": "#/definitions/prolab_accountsOAuthLoginResponse"
48+
}
49+
}
50+
},
51+
"parameters": [
4952
{
50-
"name": "challenge",
51-
"in": "query",
52-
"required": false,
53-
"type": "string"
53+
"name": "body",
54+
"in": "body",
55+
"required": true,
56+
"schema": {
57+
"$ref": "#/definitions/prolab_accountsOAuthLoginRequest"
58+
}
5459
}
5560
],
5661
"tags": [
@@ -60,6 +65,24 @@
6065
}
6166
},
6267
"definitions": {
68+
"prolab_accountsOAuthLoginRequest": {
69+
"type": "object",
70+
"properties": {
71+
"name": {
72+
"type": "string"
73+
},
74+
"password": {
75+
"type": "string"
76+
},
77+
"remember": {
78+
"type": "boolean",
79+
"format": "boolean"
80+
},
81+
"challenge": {
82+
"type": "string"
83+
}
84+
}
85+
},
6386
"prolab_accountsOAuthLoginResponse": {
6487
"type": "object",
6588
"properties": {

api/protos/oauth.proto

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ service OAuthService {
1616
}
1717
rpc OAuthLogin (OAuthLoginRequest) returns (OAuthLoginResponse) {
1818
option (google.api.http) = {
19-
get: "/oauth/login"
19+
post: "/oauth/login"
20+
body: "*"
2021
};
2122
}
2223
}

app/di/store_component.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,5 @@ func (s *storeComponentImpl) UserStore(ctx context.Context) store.UserStore {
7474
}
7575

7676
func (s *storeComponentImpl) SessionStore(ctx context.Context) store.SessionStore {
77-
return sessionstore.NewSessionStore(ctx, s.client)
77+
return sessionstore.NewSessionStore(ctx, s.client, s.db)
7878
}

app/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func Run() error {
4343
grapiserver.WithServers(
4444
server.NewSessionServiceServer(store),
4545
server.NewUserServiceServer(store),
46-
server.NewOAuthServiceServer(cli),
46+
server.NewOAuthServiceServer(cli, store),
4747
),
4848
)
4949
return s.Serve()

app/server/oauth_server.go

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@ package server
22

33
import (
44
"context"
5+
"database/sql"
6+
"strconv"
7+
"time"
58

69
"github.com/izumin5210/grapi/pkg/grapiserver"
10+
"github.com/labstack/gommon/log"
711
"github.com/ory/hydra/sdk/go/hydra/swagger"
8-
"google.golang.org/grpc/codes"
12+
"github.com/pkg/errors"
13+
"golang.org/x/crypto/bcrypt"
914
"google.golang.org/grpc/grpclog"
10-
"google.golang.org/grpc/status"
1115

1216
api_pb "github.com/ProgrammingLab/prolab-accounts/api"
1317
"github.com/ProgrammingLab/prolab-accounts/app/di"
18+
"github.com/ProgrammingLab/prolab-accounts/app/util"
1419
)
1520

1621
// OAuthServiceServer is a composite interface of api_pb.OAuthServiceServer and grapiserver.Server.
@@ -20,14 +25,16 @@ type OAuthServiceServer interface {
2025
}
2126

2227
// NewOAuthServiceServer creates a new OAuthServiceServer instance.
23-
func NewOAuthServiceServer(cli di.ClientComponent) OAuthServiceServer {
28+
func NewOAuthServiceServer(cli di.ClientComponent, store di.StoreComponent) OAuthServiceServer {
2429
return &oAuthServiceServerImpl{
2530
ClientComponent: cli,
31+
StoreComponent: store,
2632
}
2733
}
2834

2935
type oAuthServiceServerImpl struct {
3036
di.ClientComponent
37+
di.StoreComponent
3138
}
3239

3340
func (s *oAuthServiceServerImpl) StartOauthLogin(ctx context.Context, req *api_pb.StartOauthLoginRequest) (*api_pb.StartOAuthLoginResponse, error) {
@@ -59,6 +66,31 @@ func (s *oAuthServiceServerImpl) StartOauthLogin(ctx context.Context, req *api_p
5966
}
6067

6168
func (s *oAuthServiceServerImpl) OAuthLogin(ctx context.Context, req *api_pb.OAuthLoginRequest) (*api_pb.OAuthLoginResponse, error) {
62-
// TODO: Not yet implemented.
63-
return nil, status.Error(codes.Unimplemented, "TODO: You should implement it!")
69+
ss := s.SessionStore(ctx)
70+
session, err := ss.CreateSession(req.GetName(), req.GetPassword())
71+
if err != nil {
72+
if c := errors.Cause(err); c == sql.ErrNoRows || c == bcrypt.ErrMismatchedHashAndPassword {
73+
return nil, errLogin
74+
}
75+
log.Error(err)
76+
return nil, util.ErrInternalServer
77+
}
78+
79+
cli := s.HydraClient(ctx)
80+
acReq := swagger.AcceptLoginRequest{
81+
Subject: strconv.FormatInt(int64(session.UserID), 10),
82+
Remember: req.Remember,
83+
RememberFor: int64(time.Hour.Seconds()),
84+
}
85+
res, _, err := cli.AcceptLoginRequest(req.GetChallenge(), acReq)
86+
if err != nil {
87+
log.Error(err)
88+
return nil, err
89+
}
90+
91+
resp := &api_pb.OAuthLoginResponse{
92+
RedirectUrl: res.RedirectTo,
93+
}
94+
95+
return resp, nil
6496
}

app/server/sessions_server.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import (
77
"github.com/golang/protobuf/ptypes/empty"
88
"github.com/izumin5210/grapi/pkg/grapiserver"
99
"github.com/labstack/gommon/log"
10+
"github.com/pkg/errors"
1011
"golang.org/x/crypto/bcrypt"
1112
"google.golang.org/grpc/codes"
1213
"google.golang.org/grpc/status"
1314

1415
api_pb "github.com/ProgrammingLab/prolab-accounts/api"
1516
"github.com/ProgrammingLab/prolab-accounts/app/di"
1617
"github.com/ProgrammingLab/prolab-accounts/app/util"
17-
"github.com/ProgrammingLab/prolab-accounts/model"
1818
)
1919

2020
// NewSessionServiceServer creates a new SessionServiceServer instance.
@@ -39,26 +39,15 @@ func (s *sessionServiceServerImpl) GetSession(ctx context.Context, req *api_pb.G
3939
}
4040

4141
func (s *sessionServiceServerImpl) CreateSession(ctx context.Context, req *api_pb.CreateSessionRequest) (*api_pb.Session, error) {
42-
u, err := s.UserStore(ctx).FindUserByEmailOrName(req.GetName())
42+
session, err := s.SessionStore(ctx).CreateSession(req.GetName(), req.GetPassword())
4343
if err != nil {
44-
if err == sql.ErrNoRows {
44+
if c := errors.Cause(err); c == sql.ErrNoRows || c == bcrypt.ErrMismatchedHashAndPassword {
4545
return nil, errLogin
4646
}
4747
log.Error(err)
4848
return nil, util.ErrInternalServer
4949
}
5050

51-
err = bcrypt.CompareHashAndPassword([]byte(u.PasswordDigest), []byte(req.GetPassword()))
52-
if err != nil {
53-
return nil, errLogin
54-
}
55-
56-
session, err := s.SessionStore(ctx).CreateSession(model.UserID(u.ID))
57-
if err != nil {
58-
log.Error(err)
59-
return nil, util.ErrInternalServer
60-
}
61-
6251
resp := &api_pb.Session{
6352
SessionId: session.ID,
6453
}

0 commit comments

Comments
 (0)