Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

IdentityService

用户认证与管理的核心服务,负责注册、登录、用户 CRUD、密码管理。支持 Admin 和 Customer 两种用户类型,认证方式分别为 Session Cookie(Admin)和 JWT Bearer(Customer)。

RPC 列表

RPC描述权限
Register注册新用户(自动创建关联 Customer)公开
SendVerificationCode发送邮箱验证码公开
Login登录(admin 返回 session cookie,customer 返回 JWT)公开
RefreshToken刷新 access token(仅 customer)公开
ListUsers用户列表(分页+筛选)admin
GetUser获取单个用户admin
UpdateUser更新用户信息admin
DeleteUser删除用户admin-only
ChangePassword修改自己的密码已认证
AdminResetPassword管理员重置他人密码admin-only
GetMe获取当前登录用户信息已认证
Logout登出(admin 清除 session,customer 客户端删 token)已认证

Register

描述

注册新 customer 用户,自动创建关联的 Customer 记录。

Request — RegisterRequest

字段类型编号必填描述
usernamestring1用户名
emailstring2邮箱地址
display_namestring3显示名称
passwordstring4登录密码
verification_codestring5邮箱验证码

Response — RegisterResponse

字段类型编号描述
userUserInfo1新创建的用户信息
customer_idstring2自动创建的关联 Customer ID

错误码

错误说明
InvalidArgument验证码无效或已过期、用户名已存在、密码不符合要求
AlreadyExists邮箱已被注册

示例

grpcurl -plaintext \
  -d '{"username":"testuser","email":"[email protected]","display_name":"Test","password":"Pass123!","verification_code":"123456"}' \
  localhost:50051 rustbill.identity.IdentityService/Register
const resp = await api.register({
  username: "testuser",
  email: "[email protected]",
  display_name: "Test",
  password: "Pass123!",
  verification_code: "123456"
});
// resp.user, resp.customer_id

SendVerificationCode

描述

向指定邮箱发送验证码,用于注册或密码重置流程。

Request — SendVerificationCodeRequest

字段类型编号必填描述
emailstring1接收验证码的邮箱
purposestring2用途:"registration""password_reset"

Response — SendVerificationCodeResponse

字段类型编号描述
sentbool1是否发送成功
messagestring2提示信息
retry_after_secsint323多少秒后可重新发送

错误码

错误说明
InvalidArgument邮箱格式无效、发送频率过高
Internal邮件服务不可用

示例

grpcurl -plaintext \
  -d '{"email":"[email protected]","purpose":"registration"}' \
  localhost:50051 rustbill.identity.IdentityService/SendVerificationCode
const resp = await api.sendVerificationCode({
  email: "[email protected]",
  purpose: "registration"
});
// resp.sent, resp.retry_after_secs

Login

描述

用户登录。user_type="admin" 时返回 session cookie(Set-Cookie 响应头),user_type="customer" 时返回 JWT token 对。

Request — LoginRequest

字段类型编号必填描述
usernamestring1用户名
passwordstring2密码
user_typestring3用户类型:"admin""customer"

Response — LoginResponse

字段类型编号描述
access_tokenstring1JWT access token(仅 customer)
refresh_tokenstring2JWT refresh token(仅 customer)
expires_inint643token 过期时间(秒)
userUserInfo4当前用户信息
admin_pathstring5Admin 面板路径(来自服务端配置)

错误码

错误说明
InvalidArgument用户名或密码错误
PermissionDenied用户已禁用
NotFound用户不存在

示例

# Admin 登录
grpcurl -plaintext \
  -d '{"username":"admin","password":"admin123","user_type":"admin"}' \
  localhost:50051 rustbill.identity.IdentityService/Login

# Customer 登录
grpcurl -plaintext \
  -d '{"username":"customer1","password":"pass123","user_type":"customer"}' \
  localhost:50051 rustbill.identity.IdentityService/Login
const resp = await api.login({
  username: "customer1",
  password: "pass123",
  user_type: "customer"
});
// resp.access_token → 存入 localStorage
// resp.user → 存入 auth store

RefreshToken

描述

使用 refresh token 获取新的 access token(仅 customer 用户)。

Request — RefreshTokenRequest

字段类型编号必填描述
refresh_tokenstring1有效的 refresh token

Response — RefreshTokenResponse

字段类型编号描述
access_tokenstring1新的 access token
expires_inint642过期时间(秒)
refresh_tokenstring3新的 refresh token(轮换)

错误码

错误说明
InvalidArgumentrefresh token 无效或已过期

示例

grpcurl -plaintext \
  -d '{"refresh_token":"eyJhbGciOiJIUzI1NiIs..."}' \
  localhost:50051 rustbill.identity.IdentityService/RefreshToken
const resp = await api.refreshToken({
  refresh_token: localStorage.getItem("rustbill_customer_refresh")
});
// 更新 localStorage 中的 token

ListUsers

描述

分页查询用户列表,支持按角色、状态、用户类型、关键词、客户 ID 筛选。

Request — ListUsersRequest

字段类型编号必填描述
paginationPageRequest1分页参数
rolestring?2角色过滤("admin" / "operator"
is_activebool?3启用状态过滤
searchstring?4用户名/邮箱搜索
user_typestring?5用户类型:"admin""customer",不填默认 admin
customer_idstring?6按关联客户过滤(仅 user_type=“customer” 时有效)

Response — ListUsersResponse

字段类型编号描述
usersrepeated UserInfo1用户列表
metaPageMeta2分页元数据

错误码

错误说明
PermissionDenied非 admin 用户

示例

grpcurl -plaintext -H "authorization: Bearer <token>" \
  -d '{"pagination":{"page":1,"page_size":20},"user_type":"customer","is_active":true}' \
  localhost:50051 rustbill.identity.IdentityService/ListUsers
const resp = await api.listUsers({
  pagination: { page: 1, page_size: 20 },
  user_type: "customer",
  is_active: true
});

GetUser

描述

获取单个用户的详细信息。

Request — GetUserRequest

字段类型编号必填描述
idstring1用户 ID(UUID)
user_typestring?2用户类型提示,避免回退查询

Response — GetUserResponse

字段类型编号描述
userUserInfo1用户信息

错误码

错误说明
NotFound用户不存在
PermissionDenied非 admin 用户

示例

grpcurl -plaintext -H "authorization: Bearer <token>" \
  -d '{"id":"0192a123-4567-7890-abcd-ef0123456789"}' \
  localhost:50051 rustbill.identity.IdentityService/GetUser

UpdateUser

描述

更新用户信息(邮箱、显示名称、角色、状态等)。

Request — UpdateUserRequest

字段类型编号必填描述
idstring1用户 ID
emailstring?2新邮箱
display_namestring?3新显示名称
rolestring?4新角色(仅 user_type=“admin” 时有效)
is_activebool?5启用/禁用
user_typestring?6用户类型
customer_idstring?7关联客户 ID(仅 user_type=“customer” 时有效)

Response — UpdateUserResponse

字段类型编号描述
userUserInfo1更新后的用户信息

错误码

错误说明
NotFound用户不存在
InvalidArgument参数校验失败
PermissionDenied非 admin 用户

示例

grpcurl -plaintext -H "authorization: Bearer <token>" \
  -d '{"id":"0192a123-...","display_name":"New Name","is_active":true}' \
  localhost:50051 rustbill.identity.IdentityService/UpdateUser

DeleteUser

描述

删除指定用户(仅 Admin 角色可操作)。

Request — DeleteUserRequest

字段类型编号必填描述
idstring1用户 ID(UUID)

Response — DeleteUserResponse

无字段。

错误码

错误说明
NotFound用户不存在
PermissionDenied非 Admin 角色

示例

grpcurl -plaintext -H "authorization: Bearer <token>" \
  -d '{"id":"0192a123-4567-7890-abcd-ef0123456789"}' \
  localhost:50051 rustbill.identity.IdentityService/DeleteUser

ChangePassword

描述

当前登录用户修改自己的密码(需提供旧密码验证)。

Request — ChangePasswordRequest

字段类型编号必填描述
old_passwordstring1当前密码
new_passwordstring2新密码

Response — ChangePasswordResponse

无字段。

错误码

错误说明
InvalidArgument旧密码错误、新密码不符合要求
Unauthenticated未登录

示例

grpcurl -plaintext -H "authorization: Bearer <token>" \
  -d '{"old_password":"oldpass","new_password":"newpass123!"}' \
  localhost:50051 rustbill.identity.IdentityService/ChangePassword
const resp = await api.changePassword({
  old_password: "oldpass",
  new_password: "newpass123!"
});

AdminResetPassword

描述

管理员重置指定用户的密码(仅 Admin 角色可操作,无需旧密码)。

Request — AdminResetPasswordRequest

字段类型编号必填描述
user_idstring1目标用户 ID
new_passwordstring2新密码

Response — AdminResetPasswordResponse

无字段。

错误码

错误说明
NotFound用户不存在
PermissionDenied非 Admin 角色

示例

grpcurl -plaintext -H "authorization: Bearer <token>" \
  -d '{"user_id":"0192a123-...","new_password":"resetpass123!"}' \
  localhost:50051 rustbill.identity.IdentityService/AdminResetPassword

GetMe

描述

获取当前登录用户的个人信息。Admin 用户从 session cookie 恢复身份,Customer 用户从 JWT 解析。

Request — GetMeRequest

无字段。

Response — GetMeResponse

字段类型编号描述
userUserInfo1当前用户信息

错误码

错误说明
Unauthenticated未登录

示例

# Admin: cookie 自动携带
grpcurl -plaintext -H "cookie: rustbill_session=..." \
  -d '{}' \
  localhost:50051 rustbill.identity.IdentityService/GetMe

# Customer: JWT Bearer
grpcurl -plaintext -H "authorization: Bearer <token>" \
  -d '{}' \
  localhost:50051 rustbill.identity.IdentityService/GetMe
// Admin SPA (credentials: 'include' 自动携带 cookie)
const resp = await api.getMe({});
// Customer SPA
const resp = await api.getMe({});

Logout

描述

登出当前用户。Admin 端清除 session cookie,Customer 端客户端自行删除本地 token。

Request — LogoutRequest

无字段。

Response — LogoutResponse

无字段。

错误码

错误说明
Unauthenticated未登录

示例

grpcurl -plaintext -H "authorization: Bearer <token>" \
  -d '{}' \
  localhost:50051 rustbill.identity.IdentityService/Logout
const resp = await api.logout({});
// 清除 localStorage 中的 token
localStorage.removeItem("rustbill_customer_token");
localStorage.removeItem("rustbill_customer_refresh");

公共类型

UserInfo

字段类型编号描述
idstring1用户 UUID
usernamestring2用户名
emailstring3邮箱地址
display_namestring4显示名称
rolestring5角色:"admin" / "operator"(admin 用户);空字符串(customer 用户)
is_activebool6是否启用
created_atstring7创建时间(RFC3339)
user_typestring8用户类型:"admin" / "customer"
customer_idstring9关联的 Customer ID(admin 用户为空字符串)