-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1411fd1
commit 6a4c97b
Showing
5 changed files
with
288 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package auth | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"monify/internal/middlewares" | ||
|
||
monify "monify/protobuf" | ||
|
||
"github.com/google/uuid" | ||
"go.uber.org/zap" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
func get_userID(ctx context.Context, db *sql.DB, refreshToken string) (uuid.UUID, error) { | ||
|
||
query, err := db.QueryContext(ctx, ` | ||
SELECT user_id | ||
FROM user_identity | ||
WHERE refresh_token= $1`, refreshToken) | ||
defer query.Close() | ||
if err != nil { | ||
return uuid.Nil, err | ||
} | ||
if !query.Next() { | ||
return uuid.Nil, nil | ||
} | ||
var userId uuid.UUID | ||
err = query.Scan(&userId) | ||
if err != nil { | ||
return uuid.Nil, err | ||
} | ||
return userId, nil | ||
|
||
} | ||
|
||
func (s Service) RefreshToken(ctx context.Context, req *monify.RefreshTokenRequest) (*monify.RefreshTokenResponse, error) { | ||
db := ctx.Value(middlewares.StorageContextKey{}).(*sql.DB) | ||
logger := ctx.Value(middlewares.LoggerContextKey{}).(*zap.Logger) | ||
userId, err := get_userID(ctx, db, req.RefreshToken) | ||
|
||
//它給我refresh token,我要給refresh token + access token | ||
refreshToken, err := generateAndInsertRefreshToken(ctx, userId, db) | ||
if err != nil { | ||
logger.Error("failed to get refresh token", zap.Error(err)) | ||
return nil, status.Error(codes.Internal, "internal err.") | ||
} | ||
accessToken, err := GenerateAccessToken(ctx, userId, s.Secret) | ||
|
||
if err != nil { | ||
logger.Error("failed to get access token", zap.Error(err)) | ||
return nil, status.Error(codes.Internal, "internal err.") | ||
|
||
} | ||
return &monify.RefreshTokenResponse{ | ||
AccessToken: accessToken, | ||
RefreshToken: refreshToken, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,10 @@ package test | |
|
||
import ( | ||
"context" | ||
"github.com/stretchr/testify/assert" | ||
monify "monify/protobuf" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestLogin(t *testing.T) { | ||
|
@@ -18,3 +19,21 @@ func TestLogin(t *testing.T) { | |
assert.NoError(t, err) | ||
assert.Equal(t, res1.UserId, res2.UserId) | ||
} | ||
func TestRefreshToken(t *testing.T) { | ||
email := "[email protected]" | ||
client := GetTestClient(t) | ||
res1, err := client.EmailRegister(context.TODO(), &monify.EmailRegisterRequest{Email: email}) | ||
assert.NoError(t, err) | ||
res2, err := client.EmailLogin(context.TODO(), &monify.EmailLoginRequest{Email: email}) | ||
assert.NoError(t, err) | ||
assert.Equal(t, res1.UserId, res2.UserId) | ||
|
||
tokens1, err := client.RefreshToken(context.TODO(), &monify.RefreshTokenRequest{RefreshToken: res2.RefreshToken}) | ||
assert.NoError(t, err) | ||
assert.NotEmpty(t, tokens1) | ||
|
||
tokens2, err := client.RefreshToken(context.TODO(), &monify.RefreshTokenRequest{RefreshToken: tokens1.RefreshToken}) | ||
assert.NoError(t, err) | ||
assert.NotEmpty(t, tokens2) | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.