2022-12-25 16:41:38 +00:00
|
|
|
package user_test
|
2022-01-26 02:57:28 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-12-28 03:14:14 +00:00
|
|
|
"heckel.io/ntfy/user"
|
2022-01-26 02:57:28 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-02-08 03:09:31 +00:00
|
|
|
const minBcryptTimingMillis = int64(50) // Ideally should be >100ms, but this should also run on a Raspberry Pi without massive resources
|
|
|
|
|
2022-01-26 02:57:28 +00:00
|
|
|
func TestSQLiteAuth_FullScenario_Default_DenyAll(t *testing.T) {
|
|
|
|
a := newTestAuth(t, false, false)
|
2022-12-25 16:41:38 +00:00
|
|
|
require.Nil(t, a.AddUser("phil", "phil", user.RoleAdmin))
|
|
|
|
require.Nil(t, a.AddUser("ben", "ben", user.RoleUser))
|
2022-01-26 02:57:28 +00:00
|
|
|
require.Nil(t, a.AllowAccess("ben", "mytopic", true, true))
|
|
|
|
require.Nil(t, a.AllowAccess("ben", "readme", true, false))
|
|
|
|
require.Nil(t, a.AllowAccess("ben", "writeme", false, true))
|
|
|
|
require.Nil(t, a.AllowAccess("ben", "everyonewrite", false, false)) // How unfair!
|
2022-12-25 16:41:38 +00:00
|
|
|
require.Nil(t, a.AllowAccess(user.Everyone, "announcements", true, false))
|
|
|
|
require.Nil(t, a.AllowAccess(user.Everyone, "everyonewrite", true, true))
|
|
|
|
require.Nil(t, a.AllowAccess(user.Everyone, "up*", false, true)) // Everyone can write to /up*
|
2022-01-26 02:57:28 +00:00
|
|
|
|
|
|
|
phil, err := a.Authenticate("phil", "phil")
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, "phil", phil.Name)
|
2022-02-08 03:09:31 +00:00
|
|
|
require.True(t, strings.HasPrefix(phil.Hash, "$2a$10$"))
|
2022-12-25 16:41:38 +00:00
|
|
|
require.Equal(t, user.RoleAdmin, phil.Role)
|
|
|
|
require.Equal(t, []user.Grant{}, phil.Grants)
|
2022-01-26 02:57:28 +00:00
|
|
|
|
|
|
|
ben, err := a.Authenticate("ben", "ben")
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, "ben", ben.Name)
|
2022-02-08 03:09:31 +00:00
|
|
|
require.True(t, strings.HasPrefix(ben.Hash, "$2a$10$"))
|
2022-12-25 16:41:38 +00:00
|
|
|
require.Equal(t, user.RoleUser, ben.Role)
|
|
|
|
require.Equal(t, []user.Grant{
|
2022-01-26 02:57:28 +00:00
|
|
|
{"mytopic", true, true},
|
|
|
|
{"readme", true, false},
|
|
|
|
{"writeme", false, true},
|
|
|
|
{"everyonewrite", false, false},
|
|
|
|
}, ben.Grants)
|
|
|
|
|
|
|
|
notben, err := a.Authenticate("ben", "this is wrong")
|
|
|
|
require.Nil(t, notben)
|
2022-12-25 16:41:38 +00:00
|
|
|
require.Equal(t, user.ErrUnauthenticated, err)
|
2022-01-26 02:57:28 +00:00
|
|
|
|
|
|
|
// Admin can do everything
|
2022-12-25 16:41:38 +00:00
|
|
|
require.Nil(t, a.Authorize(phil, "sometopic", user.PermissionWrite))
|
|
|
|
require.Nil(t, a.Authorize(phil, "mytopic", user.PermissionRead))
|
|
|
|
require.Nil(t, a.Authorize(phil, "readme", user.PermissionWrite))
|
|
|
|
require.Nil(t, a.Authorize(phil, "writeme", user.PermissionWrite))
|
|
|
|
require.Nil(t, a.Authorize(phil, "announcements", user.PermissionWrite))
|
|
|
|
require.Nil(t, a.Authorize(phil, "everyonewrite", user.PermissionWrite))
|
2022-01-26 02:57:28 +00:00
|
|
|
|
|
|
|
// User cannot do everything
|
2022-12-25 16:41:38 +00:00
|
|
|
require.Nil(t, a.Authorize(ben, "mytopic", user.PermissionWrite))
|
|
|
|
require.Nil(t, a.Authorize(ben, "mytopic", user.PermissionRead))
|
|
|
|
require.Nil(t, a.Authorize(ben, "readme", user.PermissionRead))
|
|
|
|
require.Equal(t, user.ErrUnauthorized, a.Authorize(ben, "readme", user.PermissionWrite))
|
|
|
|
require.Equal(t, user.ErrUnauthorized, a.Authorize(ben, "writeme", user.PermissionRead))
|
|
|
|
require.Nil(t, a.Authorize(ben, "writeme", user.PermissionWrite))
|
|
|
|
require.Nil(t, a.Authorize(ben, "writeme", user.PermissionWrite))
|
|
|
|
require.Equal(t, user.ErrUnauthorized, a.Authorize(ben, "everyonewrite", user.PermissionRead))
|
|
|
|
require.Equal(t, user.ErrUnauthorized, a.Authorize(ben, "everyonewrite", user.PermissionWrite))
|
|
|
|
require.Nil(t, a.Authorize(ben, "announcements", user.PermissionRead))
|
|
|
|
require.Equal(t, user.ErrUnauthorized, a.Authorize(ben, "announcements", user.PermissionWrite))
|
2022-01-26 02:57:28 +00:00
|
|
|
|
|
|
|
// Everyone else can do barely anything
|
2022-12-25 16:41:38 +00:00
|
|
|
require.Equal(t, user.ErrUnauthorized, a.Authorize(nil, "sometopicnotinthelist", user.PermissionRead))
|
|
|
|
require.Equal(t, user.ErrUnauthorized, a.Authorize(nil, "sometopicnotinthelist", user.PermissionWrite))
|
|
|
|
require.Equal(t, user.ErrUnauthorized, a.Authorize(nil, "mytopic", user.PermissionRead))
|
|
|
|
require.Equal(t, user.ErrUnauthorized, a.Authorize(nil, "mytopic", user.PermissionWrite))
|
|
|
|
require.Equal(t, user.ErrUnauthorized, a.Authorize(nil, "readme", user.PermissionRead))
|
|
|
|
require.Equal(t, user.ErrUnauthorized, a.Authorize(nil, "readme", user.PermissionWrite))
|
|
|
|
require.Equal(t, user.ErrUnauthorized, a.Authorize(nil, "writeme", user.PermissionRead))
|
|
|
|
require.Equal(t, user.ErrUnauthorized, a.Authorize(nil, "writeme", user.PermissionWrite))
|
|
|
|
require.Equal(t, user.ErrUnauthorized, a.Authorize(nil, "announcements", user.PermissionWrite))
|
|
|
|
require.Nil(t, a.Authorize(nil, "announcements", user.PermissionRead))
|
|
|
|
require.Nil(t, a.Authorize(nil, "everyonewrite", user.PermissionRead))
|
|
|
|
require.Nil(t, a.Authorize(nil, "everyonewrite", user.PermissionWrite))
|
|
|
|
require.Nil(t, a.Authorize(nil, "up1234", user.PermissionWrite)) // Wildcard permission
|
|
|
|
require.Nil(t, a.Authorize(nil, "up5678", user.PermissionWrite))
|
2022-01-26 02:57:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSQLiteAuth_AddUser_Invalid(t *testing.T) {
|
|
|
|
a := newTestAuth(t, false, false)
|
2022-12-25 16:41:38 +00:00
|
|
|
require.Equal(t, user.ErrInvalidArgument, a.AddUser(" invalid ", "pass", user.RoleAdmin))
|
|
|
|
require.Equal(t, user.ErrInvalidArgument, a.AddUser("validuser", "pass", "invalid-role"))
|
2022-01-26 02:57:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSQLiteAuth_AddUser_Timing(t *testing.T) {
|
|
|
|
a := newTestAuth(t, false, false)
|
|
|
|
start := time.Now().UnixMilli()
|
2022-12-25 16:41:38 +00:00
|
|
|
require.Nil(t, a.AddUser("user", "pass", user.RoleAdmin))
|
2022-02-08 03:09:31 +00:00
|
|
|
require.GreaterOrEqual(t, time.Now().UnixMilli()-start, minBcryptTimingMillis)
|
2022-01-26 02:57:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSQLiteAuth_Authenticate_Timing(t *testing.T) {
|
|
|
|
a := newTestAuth(t, false, false)
|
2022-12-25 16:41:38 +00:00
|
|
|
require.Nil(t, a.AddUser("user", "pass", user.RoleAdmin))
|
2022-01-26 02:57:28 +00:00
|
|
|
|
|
|
|
// Timing a correct attempt
|
|
|
|
start := time.Now().UnixMilli()
|
|
|
|
_, err := a.Authenticate("user", "pass")
|
|
|
|
require.Nil(t, err)
|
2022-02-08 03:09:31 +00:00
|
|
|
require.GreaterOrEqual(t, time.Now().UnixMilli()-start, minBcryptTimingMillis)
|
2022-01-26 02:57:28 +00:00
|
|
|
|
|
|
|
// Timing an incorrect attempt
|
|
|
|
start = time.Now().UnixMilli()
|
|
|
|
_, err = a.Authenticate("user", "INCORRECT")
|
2022-12-25 16:41:38 +00:00
|
|
|
require.Equal(t, user.ErrUnauthenticated, err)
|
2022-02-08 03:09:31 +00:00
|
|
|
require.GreaterOrEqual(t, time.Now().UnixMilli()-start, minBcryptTimingMillis)
|
2022-01-26 02:57:28 +00:00
|
|
|
|
|
|
|
// Timing a non-existing user attempt
|
|
|
|
start = time.Now().UnixMilli()
|
|
|
|
_, err = a.Authenticate("DOES-NOT-EXIST", "hithere")
|
2022-12-25 16:41:38 +00:00
|
|
|
require.Equal(t, user.ErrUnauthenticated, err)
|
2022-02-08 03:09:31 +00:00
|
|
|
require.GreaterOrEqual(t, time.Now().UnixMilli()-start, minBcryptTimingMillis)
|
2022-01-26 02:57:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSQLiteAuth_UserManagement(t *testing.T) {
|
|
|
|
a := newTestAuth(t, false, false)
|
2022-12-25 16:41:38 +00:00
|
|
|
require.Nil(t, a.AddUser("phil", "phil", user.RoleAdmin))
|
|
|
|
require.Nil(t, a.AddUser("ben", "ben", user.RoleUser))
|
2022-01-26 02:57:28 +00:00
|
|
|
require.Nil(t, a.AllowAccess("ben", "mytopic", true, true))
|
|
|
|
require.Nil(t, a.AllowAccess("ben", "readme", true, false))
|
|
|
|
require.Nil(t, a.AllowAccess("ben", "writeme", false, true))
|
|
|
|
require.Nil(t, a.AllowAccess("ben", "everyonewrite", false, false)) // How unfair!
|
2022-12-25 16:41:38 +00:00
|
|
|
require.Nil(t, a.AllowAccess(user.Everyone, "announcements", true, false))
|
|
|
|
require.Nil(t, a.AllowAccess(user.Everyone, "everyonewrite", true, true))
|
2022-01-26 02:57:28 +00:00
|
|
|
|
|
|
|
// Query user details
|
|
|
|
phil, err := a.User("phil")
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, "phil", phil.Name)
|
2022-02-08 03:09:31 +00:00
|
|
|
require.True(t, strings.HasPrefix(phil.Hash, "$2a$10$"))
|
2022-12-25 16:41:38 +00:00
|
|
|
require.Equal(t, user.RoleAdmin, phil.Role)
|
|
|
|
require.Equal(t, []user.Grant{}, phil.Grants)
|
2022-01-26 02:57:28 +00:00
|
|
|
|
|
|
|
ben, err := a.User("ben")
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, "ben", ben.Name)
|
2022-02-08 03:09:31 +00:00
|
|
|
require.True(t, strings.HasPrefix(ben.Hash, "$2a$10$"))
|
2022-12-25 16:41:38 +00:00
|
|
|
require.Equal(t, user.RoleUser, ben.Role)
|
|
|
|
require.Equal(t, []user.Grant{
|
2022-01-26 02:57:28 +00:00
|
|
|
{"mytopic", true, true},
|
|
|
|
{"readme", true, false},
|
|
|
|
{"writeme", false, true},
|
|
|
|
{"everyonewrite", false, false},
|
|
|
|
}, ben.Grants)
|
|
|
|
|
2022-12-25 16:41:38 +00:00
|
|
|
everyone, err := a.User(user.Everyone)
|
2022-01-26 02:57:28 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, "*", everyone.Name)
|
|
|
|
require.Equal(t, "", everyone.Hash)
|
2022-12-25 16:41:38 +00:00
|
|
|
require.Equal(t, user.RoleAnonymous, everyone.Role)
|
|
|
|
require.Equal(t, []user.Grant{
|
2022-01-26 02:57:28 +00:00
|
|
|
{"announcements", true, false},
|
|
|
|
{"everyonewrite", true, true},
|
|
|
|
}, everyone.Grants)
|
|
|
|
|
|
|
|
// Ben: Before revoking
|
|
|
|
require.Nil(t, a.AllowAccess("ben", "mytopic", true, true))
|
|
|
|
require.Nil(t, a.AllowAccess("ben", "readme", true, false))
|
|
|
|
require.Nil(t, a.AllowAccess("ben", "writeme", false, true))
|
2022-12-25 16:41:38 +00:00
|
|
|
require.Nil(t, a.Authorize(ben, "mytopic", user.PermissionRead))
|
|
|
|
require.Nil(t, a.Authorize(ben, "mytopic", user.PermissionWrite))
|
|
|
|
require.Nil(t, a.Authorize(ben, "readme", user.PermissionRead))
|
|
|
|
require.Nil(t, a.Authorize(ben, "writeme", user.PermissionWrite))
|
2022-01-26 02:57:28 +00:00
|
|
|
|
|
|
|
// Revoke access for "ben" to "mytopic", then check again
|
|
|
|
require.Nil(t, a.ResetAccess("ben", "mytopic"))
|
2022-12-25 16:41:38 +00:00
|
|
|
require.Equal(t, user.ErrUnauthorized, a.Authorize(ben, "mytopic", user.PermissionWrite)) // Revoked
|
|
|
|
require.Equal(t, user.ErrUnauthorized, a.Authorize(ben, "mytopic", user.PermissionRead)) // Revoked
|
|
|
|
require.Nil(t, a.Authorize(ben, "readme", user.PermissionRead)) // Unchanged
|
|
|
|
require.Nil(t, a.Authorize(ben, "writeme", user.PermissionWrite)) // Unchanged
|
2022-01-26 02:57:28 +00:00
|
|
|
|
|
|
|
// Revoke rest of the access
|
|
|
|
require.Nil(t, a.ResetAccess("ben", ""))
|
2022-12-25 16:41:38 +00:00
|
|
|
require.Equal(t, user.ErrUnauthorized, a.Authorize(ben, "readme", user.PermissionRead)) // Revoked
|
|
|
|
require.Equal(t, user.ErrUnauthorized, a.Authorize(ben, "wrtiteme", user.PermissionWrite)) // Revoked
|
2022-01-26 02:57:28 +00:00
|
|
|
|
|
|
|
// User list
|
|
|
|
users, err := a.Users()
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, 3, len(users))
|
|
|
|
require.Equal(t, "phil", users[0].Name)
|
|
|
|
require.Equal(t, "ben", users[1].Name)
|
|
|
|
require.Equal(t, "*", users[2].Name)
|
|
|
|
|
|
|
|
// Remove user
|
|
|
|
require.Nil(t, a.RemoveUser("ben"))
|
|
|
|
_, err = a.User("ben")
|
2022-12-25 16:41:38 +00:00
|
|
|
require.Equal(t, user.ErrNotFound, err)
|
2022-01-26 02:57:28 +00:00
|
|
|
|
|
|
|
users, err = a.Users()
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, 2, len(users))
|
|
|
|
require.Equal(t, "phil", users[0].Name)
|
|
|
|
require.Equal(t, "*", users[1].Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSQLiteAuth_ChangePassword(t *testing.T) {
|
|
|
|
a := newTestAuth(t, false, false)
|
2022-12-25 16:41:38 +00:00
|
|
|
require.Nil(t, a.AddUser("phil", "phil", user.RoleAdmin))
|
2022-01-26 02:57:28 +00:00
|
|
|
|
|
|
|
_, err := a.Authenticate("phil", "phil")
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
require.Nil(t, a.ChangePassword("phil", "newpass"))
|
|
|
|
_, err = a.Authenticate("phil", "phil")
|
2022-12-25 16:41:38 +00:00
|
|
|
require.Equal(t, user.ErrUnauthenticated, err)
|
2022-01-26 02:57:28 +00:00
|
|
|
_, err = a.Authenticate("phil", "newpass")
|
|
|
|
require.Nil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSQLiteAuth_ChangeRole(t *testing.T) {
|
|
|
|
a := newTestAuth(t, false, false)
|
2022-12-25 16:41:38 +00:00
|
|
|
require.Nil(t, a.AddUser("ben", "ben", user.RoleUser))
|
2022-01-26 02:57:28 +00:00
|
|
|
require.Nil(t, a.AllowAccess("ben", "mytopic", true, true))
|
|
|
|
require.Nil(t, a.AllowAccess("ben", "readme", true, false))
|
|
|
|
|
|
|
|
ben, err := a.User("ben")
|
|
|
|
require.Nil(t, err)
|
2022-12-25 16:41:38 +00:00
|
|
|
require.Equal(t, user.RoleUser, ben.Role)
|
2022-01-26 02:57:28 +00:00
|
|
|
require.Equal(t, 2, len(ben.Grants))
|
|
|
|
|
2022-12-25 16:41:38 +00:00
|
|
|
require.Nil(t, a.ChangeRole("ben", user.RoleAdmin))
|
2022-01-26 02:57:28 +00:00
|
|
|
|
|
|
|
ben, err = a.User("ben")
|
|
|
|
require.Nil(t, err)
|
2022-12-25 16:41:38 +00:00
|
|
|
require.Equal(t, user.RoleAdmin, ben.Role)
|
2022-01-26 02:57:28 +00:00
|
|
|
require.Equal(t, 0, len(ben.Grants))
|
|
|
|
}
|
|
|
|
|
2022-12-28 03:14:14 +00:00
|
|
|
func newTestAuth(t *testing.T, defaultRead, defaultWrite bool) *user.Manager {
|
2022-01-26 02:57:28 +00:00
|
|
|
filename := filepath.Join(t.TempDir(), "user.db")
|
2022-12-28 03:14:14 +00:00
|
|
|
a, err := user.NewManager(filename, defaultRead, defaultWrite)
|
2022-01-26 02:57:28 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
return a
|
|
|
|
}
|