From 0618133e6c4fbe9ab1c8121933a5e5b0381ac8ac Mon Sep 17 00:00:00 2001 From: Lz Date: Wed, 17 Apr 2024 17:34:45 +0800 Subject: [PATCH] fix(session): renamed Login/Logout/Register --- auth.go | 16 ++--- auth_db.go | 36 +++++----- auth_signin.go => auth_login.go | 16 ++--- auth_signin_test.go => auth_login_test.go | 12 ++-- auth_login_with_code.go | 68 +++++++++++++++++++ ...de_test.go => auth_login_with_code_test.go | 20 +++--- ...gnin_with_otp.go => auth_login_with_otp.go | 12 ++-- ...otp_test.go => auth_login_with_otp_test.go | 12 ++-- auth_register.go | 13 ++++ auth_session.go | 10 +-- auth_session_test.go | 6 +- auth_signin_with_code.go | 68 ------------------- auth_signup.go | 13 ---- login_option.go | 7 ++ .../{10_signin_log.sql => 10_login_log.sql} | 2 +- ..._signin_log.sqlite => 10_login_log.sqlite} | 4 +- migration/0.0.1/11_user_token.sql | 2 + migration/0.0.1/11_user_token.sqlite | 2 + ...8_signin_openid.sql => 8_login_openid.sql} | 2 +- ...in_openid.sqlite => 8_login_openid.sqlite} | 4 +- .../{9_signin_code.sql => 9_login_code.sql} | 4 +- ...signin_code.sqlite => 9_login_code.sqlite} | 4 +- option.go | 8 +-- 23 files changed, 175 insertions(+), 166 deletions(-) rename auth_signin.go => auth_login.go (55%) rename auth_signin_test.go => auth_login_test.go (92%) create mode 100644 auth_login_with_code.go rename auth_signin_with_code_test.go => auth_login_with_code_test.go (76%) rename auth_signin_with_otp.go => auth_login_with_otp.go (59%) rename auth_signin_with_otp_test.go => auth_login_with_otp_test.go (91%) create mode 100644 auth_register.go delete mode 100644 auth_signin_with_code.go delete mode 100644 auth_signup.go rename migration/0.0.1/{10_signin_log.sql => 10_login_log.sql} (86%) rename migration/0.0.1/{10_signin_log.sqlite => 10_login_log.sqlite} (62%) rename migration/0.0.1/{8_signin_openid.sql => 8_login_openid.sql} (81%) rename migration/0.0.1/{8_signin_openid.sqlite => 8_login_openid.sqlite} (61%) rename migration/0.0.1/{9_signin_code.sql => 9_login_code.sql} (66%) rename migration/0.0.1/{9_signin_code.sqlite => 9_login_code.sqlite} (66%) diff --git a/auth.go b/auth.go index 096864b..8930d4d 100644 --- a/auth.go +++ b/auth.go @@ -23,8 +23,8 @@ var ( defaultTOPTAccountName = "Auth" defaultDHTEmail = "auth:email" defaultDHTMobile = "auth:mobile" - defaultSignInCodeLen = 6 - defaultSignInCodeTTL = 60 * time.Second + defaultLoginCodeLen = 6 + defaultLoginCodeTTL = 60 * time.Second ) var ( @@ -48,8 +48,8 @@ type Auth struct { totpIssuer string totpAccountName string - signInCodeLen int - signInCodeTTL time.Duration + loginCodeSize int + loginCodeTTL time.Duration dhtEmail string dhtMobile string @@ -122,12 +122,12 @@ func New(db *sqle.DB, options ...Option) *Auth { a.dhtMobile = defaultDHTMobile } - if a.signInCodeLen < 1 { - a.signInCodeLen = defaultSignInCodeLen + if a.loginCodeSize < 1 { + a.loginCodeSize = defaultLoginCodeLen } - if a.signInCodeTTL < 1 { - a.signInCodeTTL = defaultSignInCodeTTL + if a.loginCodeTTL < 1 { + a.loginCodeTTL = defaultLoginCodeTTL } return a diff --git a/auth_db.go b/auth_db.go index 1b0f4c5..8587e44 100644 --- a/auth_db.go +++ b/auth_db.go @@ -709,23 +709,23 @@ func (a *Auth) getUserProfileData(ctx context.Context, userID shardid.ID) (Profi return pd, nil } -func (a *Auth) createSignInCode(ctx context.Context, userID shardid.ID, ip string) (string, error) { - code := randStr(a.signInCodeLen, dicNumber) +func (a *Auth) createLoginCode(ctx context.Context, userID shardid.ID, userIP string) (string, error) { + code := randStr(a.loginCodeSize, dicNumber) now := time.Now() _, err := a.db.On(userID). ExecBuilder(ctx, a.createBuilder(). - Insert("signin_code"). + Insert("login_code"). Set("user_id", userID.Int64). Set("hash", generateHash(a.hash(), code, "")). - Set("ip", ip). - Set("expires_on", now.Add(a.signInCodeTTL)). + Set("user_ip", userIP). + Set("expires_on", now.Add(a.loginCodeTTL)). Set("created_at", now). End()) if err != nil { - a.logger.Error("auth: createSignInCode", + a.logger.Error("auth: createloginCode", slog.Int64("user_id", userID.Int64), slog.Any("err", err)) return "", ErrBadDatabase @@ -733,37 +733,33 @@ func (a *Auth) createSignInCode(ctx context.Context, userID shardid.ID, ip strin return code, nil } -func (a *Auth) checkSignInCode(ctx context.Context, userID shardid.ID, code string) error { +func (a *Auth) getLoginCodeUserIP(ctx context.Context, userID shardid.ID, code string) (string, error) { h := generateHash(a.hash(), code, "") - var count int + var userIP string err := a.db.On(userID). QueryRowBuilder(ctx, a.createBuilder(). - Select("signin_code", "count(user_id)"). + Select("login_code", "user_ip"). Where("user_id = {user_id} AND hash = {hash}"). Param("user_id", userID.Int64). Param("hash", h)). - Scan(&count) + Scan(&userIP) if err != nil { if errors.Is(err, sql.ErrNoRows) { - return ErrCodeNotMatched + return "", ErrCodeNotMatched } - a.logger.Error("auth: checkSignInCode", + a.logger.Error("auth: checkloginCode", slog.Int64("user_id", userID.Int64), slog.String("code", code), slog.Any("err", err)) - return ErrBadDatabase - } - - if count == 0 { - return ErrCodeNotMatched + return "", ErrBadDatabase } - return nil + return userIP, nil } -func (a *Auth) createSession(ctx context.Context, userID shardid.ID, firstName, lastName string) (Session, error) { +func (a *Auth) createSession(ctx context.Context, userID shardid.ID, firstName, lastName, userIP, userAgent string) (Session, error) { s := Session{ UserID: userID.Int64, FirstName: firstName, @@ -810,6 +806,8 @@ func (a *Auth) createSession(ctx context.Context, userID shardid.ID, firstName, Insert("user_token"). Set("user_id", userID.Int64). Set("hash", hashToken(s.RefreshToken)). + Set("user_ip", userID). + Set("user_agent", userAgent). Set("expires_on", exp). Set("created_at", now). End()) diff --git a/auth_signin.go b/auth_login.go similarity index 55% rename from auth_signin.go rename to auth_login.go index 076dc55..e08e2e7 100644 --- a/auth_signin.go +++ b/auth_login.go @@ -5,13 +5,13 @@ import ( "errors" ) -// SignIn sign in with email and password. -func (a *Auth) SignIn(ctx context.Context, email, passwd string, option LoginOption) (Session, error) { +// Login sign in with email and password. +func (a *Auth) Login(ctx context.Context, email, passwd string, option LoginOption) (Session, error) { u, err := a.getUserByEmail(ctx, email) if err == nil { if verifyHash(a.hash(), u.Passwd, passwd, u.Salt) { - return a.createSession(ctx, u.ID, u.FirstName, u.LastName) + return a.createSession(ctx, u.ID, u.FirstName, u.LastName, option.UserIP, option.UserAgent) } return noSession, ErrPasswdNotMatched @@ -23,20 +23,20 @@ func (a *Auth) SignIn(ctx context.Context, email, passwd string, option LoginOpt return noSession, err } - return a.createSession(ctx, u.ID, u.FirstName, u.LastName) + return a.createSession(ctx, u.ID, u.FirstName, u.LastName, option.UserIP, option.UserAgent) } return noSession, err } -// SignInMobile sign in with mobile and password. -func (a *Auth) SignInMobile(ctx context.Context, mobile, passwd string, option LoginOption) (Session, error) { +// LoginMobile sign in with mobile and password. +func (a *Auth) LoginMobile(ctx context.Context, mobile, passwd string, option LoginOption) (Session, error) { u, err := a.getUserByMobile(ctx, mobile) if err == nil { if verifyHash(a.hash(), u.Passwd, passwd, u.Salt) { - return a.createSession(ctx, u.ID, u.FirstName, u.LastName) + return a.createSession(ctx, u.ID, u.FirstName, u.LastName, option.UserIP, option.UserAgent) } return noSession, ErrPasswdNotMatched @@ -48,7 +48,7 @@ func (a *Auth) SignInMobile(ctx context.Context, mobile, passwd string, option L return noSession, err } - return a.createSession(ctx, u.ID, u.FirstName, u.LastName) + return a.createSession(ctx, u.ID, u.FirstName, u.LastName, option.UserIP, option.UserAgent) } return noSession, err diff --git a/auth_signin_test.go b/auth_login_test.go similarity index 92% rename from auth_signin_test.go rename to auth_login_test.go index 6a81cfa..d80fda4 100644 --- a/auth_signin_test.go +++ b/auth_login_test.go @@ -8,9 +8,9 @@ import ( "github.com/yaitoo/sqle/shardid" ) -func TestSignIn(t *testing.T) { +func TestLogin(t *testing.T) { - authTest := createAuthTest("./tests_sign_in.db") + authTest := createAuthTest("./tests_login.db") tests := []struct { name string @@ -73,7 +73,7 @@ func TestSignIn(t *testing.T) { } } - s, err := authTest.SignIn(context.TODO(), test.email, test.passwd, test.option) + s, err := authTest.Login(context.TODO(), test.email, test.passwd, test.option) if test.wantedErr == nil { require.NoError(t, err) } else { @@ -97,9 +97,9 @@ func TestSignIn(t *testing.T) { } } -func TestSignInMobile(t *testing.T) { +func TestLoginMobile(t *testing.T) { - authTest := createAuthTest("./tests_sign_in_mobile.db") + authTest := createAuthTest("./tests_login_mobile.db") tests := []struct { name string @@ -173,7 +173,7 @@ func TestSignInMobile(t *testing.T) { } } - s, err := authTest.SignInMobile(context.TODO(), test.mobile, test.passwd, test.option) + s, err := authTest.LoginMobile(context.TODO(), test.mobile, test.passwd, test.option) if test.wantedErr == nil { require.NoError(t, err) } else { diff --git a/auth_login_with_code.go b/auth_login_with_code.go new file mode 100644 index 0000000..dd0730f --- /dev/null +++ b/auth_login_with_code.go @@ -0,0 +1,68 @@ +package auth + +import ( + "context" + "errors" +) + +// CreateLoginCode create a code for loging in by email +func (a *Auth) CreateLoginCode(ctx context.Context, email string, option LoginOption) (string, error) { + id, err := a.getUserIDByEmail(ctx, email) + + if option.CreateIfNotExists && errors.Is(err, ErrEmailNotFound) { + u, err := a.createLoginWithEmail(ctx, email, randStr(12, dicAlphaNumber), option.FirstName, option.LastName) + if err != nil { + return "", err + } + + id = u.ID + } + + return a.createLoginCode(ctx, id, option.UserIP) +} + +// LoginWithCode sign in with email and code. +func (a *Auth) LoginWithCode(ctx context.Context, email, code string) (Session, error) { + u, err := a.getUserByEmail(ctx, email) + if err != nil { + return noSession, err + } + + userIP, err := a.getLoginCodeUserIP(ctx, u.ID, code) + if err != nil { + return noSession, err + } + + return a.createSession(ctx, u.ID, u.FirstName, u.LastName, userIP, "CODE") +} + +// CreateLoginMobileCode create a code for loging in by mobile +func (a *Auth) CreateLoginMobileCode(ctx context.Context, mobile string, option LoginOption) (string, error) { + id, err := a.getUserIDByMobile(ctx, mobile) + + if option.CreateIfNotExists && errors.Is(err, ErrMobileNotFound) { + u, err := a.createLoginWithMobile(ctx, mobile, randStr(12, dicAlphaNumber), option.FirstName, option.LastName) + if err != nil { + return "", err + } + + id = u.ID + } + + return a.createLoginCode(ctx, id, option.UserIP) +} + +// LoginMobileWithCode sign in with mobile and code. +func (a *Auth) LoginMobileWithCode(ctx context.Context, mobile, code string) (Session, error) { + u, err := a.getUserByMobile(ctx, mobile) + if err != nil { + return noSession, err + } + + userIP, err := a.getLoginCodeUserIP(ctx, u.ID, code) + if err != nil { + return noSession, err + } + + return a.createSession(ctx, u.ID, u.FirstName, u.LastName, userIP, "CODE") +} diff --git a/auth_signin_with_code_test.go b/auth_login_with_code_test.go similarity index 76% rename from auth_signin_with_code_test.go rename to auth_login_with_code_test.go index 111a96b..c635be3 100644 --- a/auth_signin_with_code_test.go +++ b/auth_login_with_code_test.go @@ -8,9 +8,9 @@ import ( "github.com/yaitoo/sqle/shardid" ) -func TestSignInWithCode(t *testing.T) { +func TestLoginWithCode(t *testing.T) { - authTest := createAuthTest("./tests_sign_in_with_code.db") + authTest := createAuthTest("./tests_login_with_code.db") tests := []struct { name string @@ -32,7 +32,7 @@ func TestSignInWithCode(t *testing.T) { email: "code_not_matched@sign_in_with_code.com", wantedErr: ErrCodeNotMatched, setup: func(r *require.Assertions) string { - _, err := authTest.CreateSignInCode(context.Background(), "code_not_matched@sign_in_with_code.com", LoginOption{CreateIfNotExists: true}) + _, err := authTest.CreateLoginCode(context.Background(), "code_not_matched@sign_in_with_code.com", LoginOption{CreateIfNotExists: true}) r.NoError(err) return "" @@ -42,7 +42,7 @@ func TestSignInWithCode(t *testing.T) { name: "code_should_work", email: "code@sign_in_with_code.com", setup: func(r *require.Assertions) string { - code, err := authTest.CreateSignInCode(context.Background(), "code@sign_in_with_code.com", LoginOption{CreateIfNotExists: true}) + code, err := authTest.CreateLoginCode(context.Background(), "code@sign_in_with_code.com", LoginOption{CreateIfNotExists: true}) r.NoError(err) return code @@ -58,7 +58,7 @@ func TestSignInWithCode(t *testing.T) { code := test.setup(r) - s, err := authTest.SignInWithCode(context.TODO(), test.email, code) + s, err := authTest.LoginWithCode(context.TODO(), test.email, code) if test.wantedErr == nil { require.NoError(t, err) } else { @@ -82,9 +82,9 @@ func TestSignInWithCode(t *testing.T) { } } -func TestSignInMobileWithCode(t *testing.T) { +func TestLoginMobileWithCode(t *testing.T) { - authTest := createAuthTest("./tests_sign_in_mobile_with_code.db") + authTest := createAuthTest("./tests_login_mobile_with_code.db") tests := []struct { name string @@ -106,7 +106,7 @@ func TestSignInMobileWithCode(t *testing.T) { mobile: "1+333444555", wantedErr: ErrCodeNotMatched, setup: func(r *require.Assertions) string { - _, err := authTest.CreateSignInMobileCode(context.Background(), "1+333444555", LoginOption{CreateIfNotExists: true}) + _, err := authTest.CreateLoginMobileCode(context.Background(), "1+333444555", LoginOption{CreateIfNotExists: true}) r.NoError(err) return "" @@ -116,7 +116,7 @@ func TestSignInMobileWithCode(t *testing.T) { name: "code_should_work", mobile: "1+444555666", setup: func(r *require.Assertions) string { - code, err := authTest.CreateSignInMobileCode(context.Background(), "1+444555666", LoginOption{CreateIfNotExists: true}) + code, err := authTest.CreateLoginMobileCode(context.Background(), "1+444555666", LoginOption{CreateIfNotExists: true}) r.NoError(err) return code @@ -131,7 +131,7 @@ func TestSignInMobileWithCode(t *testing.T) { code := test.setup(r) - s, err := authTest.SignInMobileWithCode(context.TODO(), test.mobile, code) + s, err := authTest.LoginMobileWithCode(context.TODO(), test.mobile, code) if test.wantedErr == nil { require.NoError(t, err) } else { diff --git a/auth_signin_with_otp.go b/auth_login_with_otp.go similarity index 59% rename from auth_signin_with_otp.go rename to auth_login_with_otp.go index a7cea2d..38e0955 100644 --- a/auth_signin_with_otp.go +++ b/auth_login_with_otp.go @@ -6,8 +6,8 @@ import ( "github.com/pquerna/otp/totp" ) -// SignInWithOTP sign in with email and otp. -func (a *Auth) SignInWithOTP(ctx context.Context, email, otp string) (Session, error) { +// LoginWithOTP sign in with email and otp. +func (a *Auth) LoginWithOTP(ctx context.Context, email, otp string) (Session, error) { u, err := a.getUserByEmail(ctx, email) @@ -24,12 +24,12 @@ func (a *Auth) SignInWithOTP(ctx context.Context, email, otp string) (Session, e return noSession, ErrOTPNotMatched } - return a.createSession(ctx, u.ID, u.FirstName, u.LastName) + return a.createSession(ctx, u.ID, u.FirstName, u.LastName, "", "OTP") } -// SignInMobileWithOTP sign in with mobile and otp. -func (a *Auth) SignInMobileWithOTP(ctx context.Context, mobile, otp string) (Session, error) { +// LoginMobileWithOTP sign in with mobile and otp. +func (a *Auth) LoginMobileWithOTP(ctx context.Context, mobile, otp string) (Session, error) { u, err := a.getUserByMobile(ctx, mobile) if err != nil { @@ -45,5 +45,5 @@ func (a *Auth) SignInMobileWithOTP(ctx context.Context, mobile, otp string) (Ses return noSession, ErrOTPNotMatched } - return a.createSession(ctx, u.ID, u.FirstName, u.LastName) + return a.createSession(ctx, u.ID, u.FirstName, u.LastName, "", "OTP") } diff --git a/auth_signin_with_otp_test.go b/auth_login_with_otp_test.go similarity index 91% rename from auth_signin_with_otp_test.go rename to auth_login_with_otp_test.go index 25ffd84..3beee24 100644 --- a/auth_signin_with_otp_test.go +++ b/auth_login_with_otp_test.go @@ -10,9 +10,9 @@ import ( "github.com/yaitoo/sqle/shardid" ) -func TestSignInWithOTP(t *testing.T) { +func TestLoginWithOTP(t *testing.T) { - authTest := createAuthTest("./tests_sign_in_with_otp.db") + authTest := createAuthTest("./tests_login_with_otp.db") tests := []struct { name string @@ -65,7 +65,7 @@ func TestSignInWithOTP(t *testing.T) { code := test.setup(r) - s, err := authTest.SignInWithOTP(context.TODO(), test.email, code) + s, err := authTest.LoginWithOTP(context.TODO(), test.email, code) if test.wantedErr == nil { require.NoError(t, err) } else { @@ -89,9 +89,9 @@ func TestSignInWithOTP(t *testing.T) { } } -func TestSignInMobileWithOTP(t *testing.T) { +func TestLoginMobileWithOTP(t *testing.T) { - authTest := createAuthTest("./tests_sign_in_mobile_with_otp.db") + authTest := createAuthTest("./tests_login_mobile_with_otp.db") tests := []struct { name string @@ -144,7 +144,7 @@ func TestSignInMobileWithOTP(t *testing.T) { code := test.setup(r) - s, err := authTest.SignInMobileWithOTP(context.TODO(), test.mobile, code) + s, err := authTest.LoginMobileWithOTP(context.TODO(), test.mobile, code) if test.wantedErr == nil { require.NoError(t, err) } else { diff --git a/auth_register.go b/auth_register.go new file mode 100644 index 0000000..b792b89 --- /dev/null +++ b/auth_register.go @@ -0,0 +1,13 @@ +package auth + +import "context" + +// Register sign up with email +func (a *Auth) Register(ctx context.Context, email, passwd, firstName, lastName string) (User, error) { + return a.createLoginWithEmail(ctx, email, passwd, firstName, lastName) +} + +// RegisterMobile sign up with mobile +func (a *Auth) RegisterMobile(ctx context.Context, mobile, passwd, firstName, lastName string) (User, error) { + return a.createLoginWithMobile(ctx, mobile, passwd, firstName, lastName) +} diff --git a/auth_session.go b/auth_session.go index 563b4ff..14d99af 100644 --- a/auth_session.go +++ b/auth_session.go @@ -9,8 +9,8 @@ import ( var noUserID shardid.ID -// SignOut sign out the user, and delete his refresh token -func (a *Auth) SignOut(ctx context.Context, uid shardid.ID) error { +// Logout sign out the user, and delete his refresh token +func (a *Auth) Logout(ctx context.Context, uid shardid.ID) error { return a.deleteUserToken(ctx, uid, "") } @@ -35,7 +35,7 @@ func (a *Auth) IsAuthenticated(ctx context.Context, accessToken string) (shardid } // RefreshSession refresh access token and refresh token -func (a *Auth) RefreshSession(ctx context.Context, refreshToken string) (Session, error) { +func (a *Auth) RefreshSession(ctx context.Context, refreshToken string, clientInfo ClientInfo) (Session, error) { token, err := jwt.ParseWithClaims(refreshToken, &UserClaims{}, func(token *jwt.Token) (interface{}, error) { return a.jwtSignKey, nil }) @@ -57,12 +57,12 @@ func (a *Auth) RefreshSession(ctx context.Context, refreshToken string) (Session return noSession, err } - go a.deleteUserToken(ctx, uid, refreshToken) // nolint: errcheck + a.deleteUserToken(ctx, uid, refreshToken) // nolint: errcheck u, err := a.getUserByID(ctx, uid) if err != nil { return noSession, err } - return a.createSession(ctx, uid, u.FirstName, u.FirstName) + return a.createSession(ctx, uid, u.FirstName, u.FirstName, clientInfo.UserIP, clientInfo.UserAgent) } diff --git a/auth_session_test.go b/auth_session_test.go index dac722a..a857f67 100644 --- a/auth_session_test.go +++ b/auth_session_test.go @@ -11,7 +11,7 @@ import ( func TestSession(t *testing.T) { au := createAuthTest("./tests_session.db") - s, err := au.SignIn(context.TODO(), "u@session.com", "abc123", LoginOption{CreateIfNotExists: true}) + s, err := au.Login(context.TODO(), "u@session.com", "abc123", LoginOption{CreateIfNotExists: true}) require.NoError(t, err) uid := shardid.Parse(s.UserID) @@ -19,7 +19,7 @@ func TestSession(t *testing.T) { require.NoError(t, err) // refresh token should be refreshed - rs, err := au.RefreshSession(context.Background(), s.RefreshToken) + rs, err := au.RefreshSession(context.Background(), s.RefreshToken, ClientInfo{}) require.NoError(t, err) err = au.checkRefreshToken(context.Background(), uid, rs.RefreshToken) require.NoError(t, err) @@ -28,7 +28,7 @@ func TestSession(t *testing.T) { require.ErrorIs(t, err, ErrInvalidToken) // sign out should delete all tokens - err = au.SignOut(context.Background(), uid) + err = au.Logout(context.Background(), uid) require.NoError(t, err) err = au.checkRefreshToken(context.Background(), uid, rs.RefreshToken) diff --git a/auth_signin_with_code.go b/auth_signin_with_code.go deleted file mode 100644 index 588ff52..0000000 --- a/auth_signin_with_code.go +++ /dev/null @@ -1,68 +0,0 @@ -package auth - -import ( - "context" - "errors" -) - -// CreateSignInCode create a code for signing in by email -func (a *Auth) CreateSignInCode(ctx context.Context, email string, option LoginOption) (string, error) { - id, err := a.getUserIDByEmail(ctx, email) - - if option.CreateIfNotExists && errors.Is(err, ErrEmailNotFound) { - u, err := a.createLoginWithEmail(ctx, email, randStr(12, dicAlphaNumber), option.FirstName, option.LastName) - if err != nil { - return "", err - } - - id = u.ID - } - - return a.createSignInCode(ctx, id, option.UserIP) -} - -// SignInWithCode sign in with email and code. -func (a *Auth) SignInWithCode(ctx context.Context, email, code string) (Session, error) { - u, err := a.getUserByEmail(ctx, email) - if err != nil { - return noSession, err - } - - err = a.checkSignInCode(ctx, u.ID, code) - if err != nil { - return noSession, err - } - - return a.createSession(ctx, u.ID, u.FirstName, u.LastName) -} - -// CreateSignInMobileCode create a code for signing in by mobile -func (a *Auth) CreateSignInMobileCode(ctx context.Context, mobile string, option LoginOption) (string, error) { - id, err := a.getUserIDByMobile(ctx, mobile) - - if option.CreateIfNotExists && errors.Is(err, ErrMobileNotFound) { - u, err := a.createLoginWithMobile(ctx, mobile, randStr(12, dicAlphaNumber), option.FirstName, option.LastName) - if err != nil { - return "", err - } - - id = u.ID - } - - return a.createSignInCode(ctx, id, option.UserIP) -} - -// SignInMobileWithCode sign in with mobile and code. -func (a *Auth) SignInMobileWithCode(ctx context.Context, mobile, code string) (Session, error) { - u, err := a.getUserByMobile(ctx, mobile) - if err != nil { - return noSession, err - } - - err = a.checkSignInCode(ctx, u.ID, code) - if err != nil { - return noSession, err - } - - return a.createSession(ctx, u.ID, u.FirstName, u.LastName) -} diff --git a/auth_signup.go b/auth_signup.go deleted file mode 100644 index 1ac609f..0000000 --- a/auth_signup.go +++ /dev/null @@ -1,13 +0,0 @@ -package auth - -import "context" - -// SignUp sign up with email -func (a *Auth) SignUp(ctx context.Context, email, passwd, firstName, lastName string) (User, error) { - return a.createLoginWithEmail(ctx, email, passwd, firstName, lastName) -} - -// SignUpMobile sign up with mobile -func (a *Auth) SignUpMobile(ctx context.Context, mobile, passwd, firstName, lastName string) (User, error) { - return a.createLoginWithMobile(ctx, mobile, passwd, firstName, lastName) -} diff --git a/login_option.go b/login_option.go index 15458b3..94724d1 100644 --- a/login_option.go +++ b/login_option.go @@ -14,3 +14,10 @@ type LoginOption struct { // LastName last name. only use when CreateIfNotExists is true LastName string } + +type ClientInfo struct { + // UserIP user's ip address + UserIP string + // UserAgent user's device info + UserAgent string +} diff --git a/migration/0.0.1/10_signin_log.sql b/migration/0.0.1/10_login_log.sql similarity index 86% rename from migration/0.0.1/10_signin_log.sql rename to migration/0.0.1/10_login_log.sql index 40037ea..c0f4cc5 100644 --- a/migration/0.0.1/10_signin_log.sql +++ b/migration/0.0.1/10_login_log.sql @@ -1,4 +1,4 @@ -CREATE TABLE IF NOT EXISTS `signin_log` ( +CREATE TABLE IF NOT EXISTS `login_log` ( `id` bigint NOT NULL, `user_id` bigint NOT NULL, `method` char(1) NOT NULL COMMENT 'E=email/password/L=password less/T=TOTP/A=oauth', diff --git a/migration/0.0.1/10_signin_log.sqlite b/migration/0.0.1/10_login_log.sqlite similarity index 62% rename from migration/0.0.1/10_signin_log.sqlite rename to migration/0.0.1/10_login_log.sqlite index 1bb859e..87e74f1 100644 --- a/migration/0.0.1/10_signin_log.sqlite +++ b/migration/0.0.1/10_login_log.sqlite @@ -1,4 +1,4 @@ -CREATE TABLE IF NOT EXISTS `signin_log` ( +CREATE TABLE IF NOT EXISTS `login_log` ( `id` bigint NOT NULL, `user_id` bigint NOT NULL, `method` char(1) NOT NULL, @@ -9,4 +9,4 @@ CREATE TABLE IF NOT EXISTS `signin_log` ( PRIMARY KEY (`id`) ); -CREATE INDEX `idx_login_log_user` ON `signin_log` (`user_id`,`created_at`); \ No newline at end of file +CREATE INDEX `idx_login_log_user` ON `login_log` (`user_id`,`created_at`); \ No newline at end of file diff --git a/migration/0.0.1/11_user_token.sql b/migration/0.0.1/11_user_token.sql index 70a02f3..fcf1933 100644 --- a/migration/0.0.1/11_user_token.sql +++ b/migration/0.0.1/11_user_token.sql @@ -1,6 +1,8 @@ CREATE TABLE IF NOT EXISTS `user_token` ( `user_id` bigint NOT NULL, `hash` varchar(255) NOT NULL, + `user_ip` varchar(39) NOT NULL, + `user_agent` varchar(255) NOT NULL, `expires_on` datetime NOT NULL, `created_at` datetime NOT NULL, PRIMARY KEY (`user_id`,`hash`) diff --git a/migration/0.0.1/11_user_token.sqlite b/migration/0.0.1/11_user_token.sqlite index 70a02f3..fcf1933 100644 --- a/migration/0.0.1/11_user_token.sqlite +++ b/migration/0.0.1/11_user_token.sqlite @@ -1,6 +1,8 @@ CREATE TABLE IF NOT EXISTS `user_token` ( `user_id` bigint NOT NULL, `hash` varchar(255) NOT NULL, + `user_ip` varchar(39) NOT NULL, + `user_agent` varchar(255) NOT NULL, `expires_on` datetime NOT NULL, `created_at` datetime NOT NULL, PRIMARY KEY (`user_id`,`hash`) diff --git a/migration/0.0.1/8_signin_openid.sql b/migration/0.0.1/8_login_openid.sql similarity index 81% rename from migration/0.0.1/8_signin_openid.sql rename to migration/0.0.1/8_login_openid.sql index 83e531e..5c31424 100644 --- a/migration/0.0.1/8_signin_openid.sql +++ b/migration/0.0.1/8_login_openid.sql @@ -1,4 +1,4 @@ -CREATE TABLE IF NOT EXISTS `signin_openid` ( +CREATE TABLE IF NOT EXISTS `login_openid` ( `hash` varchar(125) NOT NULL, `openid_user` varchar(125) NOT NULL, `openid_app` varchar(50) NOT NULL, diff --git a/migration/0.0.1/8_signin_openid.sqlite b/migration/0.0.1/8_login_openid.sqlite similarity index 61% rename from migration/0.0.1/8_signin_openid.sqlite rename to migration/0.0.1/8_login_openid.sqlite index 78bb76d..476bd7b 100644 --- a/migration/0.0.1/8_signin_openid.sqlite +++ b/migration/0.0.1/8_login_openid.sqlite @@ -1,4 +1,4 @@ -CREATE TABLE IF NOT EXISTS `signin_openid` ( +CREATE TABLE IF NOT EXISTS `login_openid` ( `hash` varchar(125) NOT NULL, `openid_user` varchar(125) NOT NULL, `openid_app` varchar(50) NOT NULL, @@ -7,4 +7,4 @@ CREATE TABLE IF NOT EXISTS `signin_openid` ( PRIMARY KEY (`hash`) ); -CREATE INDEX `idx_openid_user` ON `signin_openid` (`user_id`); \ No newline at end of file +CREATE INDEX `idx_openid_user` ON `login_openid` (`user_id`); \ No newline at end of file diff --git a/migration/0.0.1/9_signin_code.sql b/migration/0.0.1/9_login_code.sql similarity index 66% rename from migration/0.0.1/9_signin_code.sql rename to migration/0.0.1/9_login_code.sql index 7458933..7660c10 100644 --- a/migration/0.0.1/9_signin_code.sql +++ b/migration/0.0.1/9_login_code.sql @@ -1,7 +1,7 @@ -CREATE TABLE IF NOT EXISTS `signin_code` ( +CREATE TABLE IF NOT EXISTS `login_code` ( `user_id` bigint NOT NULL, `hash` varchar(256) NOT NULL, - `ip` varchar(39) NOT NULL, + `user_ip` varchar(39) NOT NULL, `expires_on` datetime NOT NULL, `created_at` datetime NOT NULL, PRIMARY KEY (`user_id`,`hash`) diff --git a/migration/0.0.1/9_signin_code.sqlite b/migration/0.0.1/9_login_code.sqlite similarity index 66% rename from migration/0.0.1/9_signin_code.sqlite rename to migration/0.0.1/9_login_code.sqlite index 2bb2742..84565f2 100644 --- a/migration/0.0.1/9_signin_code.sqlite +++ b/migration/0.0.1/9_login_code.sqlite @@ -1,7 +1,7 @@ -CREATE TABLE IF NOT EXISTS `signin_code` ( +CREATE TABLE IF NOT EXISTS `login_code` ( `user_id` bigint NOT NULL, `hash` varchar(256) NOT NULL, - `ip` varchar(39) NOT NULL, + `user_ip` varchar(39) NOT NULL, `expires_on` datetime NOT NULL, `created_at` datetime NOT NULL, PRIMARY KEY (`user_id`,`hash`) diff --git a/option.go b/option.go index 326ff92..d2633a5 100644 --- a/option.go +++ b/option.go @@ -92,10 +92,10 @@ func WithDHT(email, mobile string) Option { } } -// WithSignInCode set sign in code length and ttl -func WithSignInCode(l int, ttl time.Duration) Option { +// WithLoginCode set sign in code length and ttl +func WithLoginCode(size int, ttl time.Duration) Option { return func(a *Auth) { - a.signInCodeLen = l - a.signInCodeTTL = ttl + a.loginCodeSize = size + a.loginCodeTTL = ttl } }