extension-authorization
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAuthorization
授权
Authorization extendsion for Caffeine AI.
为Caffeine AI开发的授权扩展。
Overview
概述
This skill adds an authentication and authorization system with role-based access control using the mixin pattern. The mixin provides standard authorization endpoints automatically.
MixinAuthorization该技能通过混入模式(mixin pattern)添加了一套带有基于角色访问控制的身份认证与授权系统。混入类可自动提供标准的授权端点。
MixinAuthorizationBackend
后端
Authentication system with role-based access control.
There is a prefabricated library . It provides core authentication with role-based access control.
mo:caffeineai-authorization/access-control.mo带有基于角色访问控制的身份认证系统。
预制库提供了基于角色访问控制的核心身份认证功能。
mo:caffeineai-authorization/access-control.moModule API
模块API
mo
module {
public type UserRole = {
#admin;
#user;
#guest;
};
public type AccessControlState = { /* internal state */ };
public func initState() : AccessControlState;
public func getUserRole(state : AccessControlState, caller : Principal) : UserRole;
public func assignRole(state : AccessControlState, caller : Principal, user : Principal, role : UserRole);
public func isAdmin(state : AccessControlState, caller : Principal) : Bool;
public func hasPermission(state : AccessControlState, caller : Principal, requiredRole : UserRole) : Bool;
};Initialization is handled internally by -- do not call directly. The first authenticated user to log in automatically becomes admin; no token or secret is required.
MixinAuthorizationinitializeIMPORTANT: The line MUST be placed in , not in a custom mixin file.
include MixinAuthorization(accessControlState)main.momo
module {
public type UserRole = {
#admin;
#user;
#guest;
};
public type AccessControlState = { /* internal state */ };
public func initState() : AccessControlState;
public func getUserRole(state : AccessControlState, caller : Principal) : UserRole;
public func assignRole(state : AccessControlState, caller : Principal, user : Principal, role : UserRole);
public func isAdmin(state : AccessControlState, caller : Principal) : Bool;
public func hasPermission(state : AccessControlState, caller : Principal, requiredRole : UserRole) : Bool;
};初始化由内部处理——请勿直接调用方法。第一个完成身份认证的用户将自动成为管理员,无需令牌或密钥。
MixinAuthorizationinitialize重要提示:代码行必须放在中,而非自定义混入文件内。
include MixinAuthorization(accessControlState)main.moOpting out / custom authentication
退出/自定义身份认证
If the user asks to replace this authorization system with custom authentication (for example username/password auth, no Internet Identity, or no ), remove the mops package from the project. This is the only supported way to remove the lint requirement, because that lint rule is shipped by the package. Do not add suppression comments or leave the package installed while omitting .
MixinAuthorizationcaffeineai-authorizationinclude-authorizationinclude MixinAuthorization(accessControlState)When removing the package, also remove all imports, the initialization, , and any guard calls that belonged to this component. Replace them with the custom authentication and authorization checks requested by the user.
mo:caffeineai-authorization/*accessControlStateinclude MixinAuthorization(accessControlState)AccessControl若用户要求用自定义身份认证(例如用户名/密码认证、不使用Internet Identity或不使用)替换本授权系统,请从项目中移除 mops包。这是移除 lint检查要求的唯一支持方式,因为该lint规则由该包提供。请勿添加抑制注释,也不要在省略的情况下保留该包。
MixinAuthorizationcaffeineai-authorizationinclude-authorizationinclude MixinAuthorization(accessControlState)移除包时,还需删除所有导入、初始化代码、以及所有属于该组件的守卫调用。替换为用户要求的自定义身份认证与授权检查逻辑。
mo:caffeineai-authorization/*accessControlStateinclude MixinAuthorization(accessControlState)AccessControlSetup in main.mo
在main.mo中配置
motoko
import Map "mo:core/Map";
import Principal "mo:core/Principal";
import AccessControl "mo:caffeineai-authorization/access-control";
import MixinAuthorization "mo:caffeineai-authorization/MixinAuthorization";
import Types "types";
import ProfileMixin "mixins/Profile";
actor {
let accessControlState = AccessControl.initState();
include MixinAuthorization(accessControlState, null);
let userProfiles = Map.empty<Principal, Types.UserProfile>();
include ProfileMixin(accessControlState, userProfiles);
};motoko
import Map "mo:core/Map";
import Principal "mo:core/Principal";
import AccessControl "mo:caffeineai-authorization/access-control";
import MixinAuthorization "mo:caffeineai-authorization/MixinAuthorization";
import Types "types";
import ProfileMixin "mixins/Profile";
actor {
let accessControlState = AccessControl.initState();
include MixinAuthorization(accessControlState, null);
let userProfiles = Map.empty<Principal, Types.UserProfile>();
include ProfileMixin(accessControlState, userProfiles);
};Type Definitions in types.mo
types.mo中的类型定义
motoko
module {
public type UserProfile = {
name : Text;
};
};motoko
module {
public type UserProfile = {
name : Text;
};
};Custom Mixin Example (mixins/Profile.mo)
自定义混入示例(mixins/Profile.mo)
The frontend requires , , and . Pass to your mixin so it can check permissions.
getCallerUserProfilesaveCallerUserProfilegetUserProfileaccessControlStatemotoko
import Map "mo:core/Map";
import Principal "mo:core/Principal";
import Runtime "mo:core/Runtime";
import AccessControl "mo:caffeineai-authorization/access-control";
import Types "../types";
mixin (
accessControlState : AccessControl.AccessControlState,
userProfiles : Map.Map<Principal, Types.UserProfile>,
) {
public query ({ caller }) func getCallerUserProfile() : async ?Types.UserProfile {
if (not AccessControl.hasPermission(accessControlState, caller, #user)) {
Runtime.trap("Unauthorized");
};
userProfiles.get(caller);
};
public shared ({ caller }) func saveCallerUserProfile(profile : Types.UserProfile) : async () {
if (not AccessControl.hasPermission(accessControlState, caller, #user)) {
Runtime.trap("Unauthorized");
};
userProfiles.add(caller, profile);
};
public query ({ caller }) func getUserProfile(user : Principal) : async ?Types.UserProfile {
if (caller != user and not AccessControl.isAdmin(accessControlState, caller)) {
Runtime.trap("Unauthorized: Can only view your own profile");
};
userProfiles.get(user);
};
};前端需要、和方法。将传入混入类以检查权限。
getCallerUserProfilesaveCallerUserProfilegetUserProfileaccessControlStatemotoko
import Map "mo:core/Map";
import Principal "mo:core/Principal";
import Runtime "mo:core/Runtime";
import AccessControl "mo:caffeineai-authorization/access-control";
import Types "../types";
mixin (
accessControlState : AccessControl.AccessControlState,
userProfiles : Map.Map<Principal, Types.UserProfile>,
) {
public query ({ caller }) func getCallerUserProfile() : async ?Types.UserProfile {
if (not AccessControl.hasPermission(accessControlState, caller, #user)) {
Runtime.trap("Unauthorized");
};
userProfiles.get(caller);
};
public shared ({ caller }) func saveCallerUserProfile(profile : Types.UserProfile) : async () {
if (not AccessControl.hasPermission(accessControlState, caller, #user)) {
Runtime.trap("Unauthorized");
};
userProfiles.add(caller, profile);
};
public query ({ caller }) func getUserProfile(user : Principal) : async ?Types.UserProfile {
if (caller != user and not AccessControl.isAdmin(accessControlState, caller)) {
Runtime.trap("Unauthorized: Can only view your own profile");
};
userProfiles.get(user);
};
};Guard Patterns
守卫模式
Apply the appropriate guard to every public function:
// Admin-only:
if (not AccessControl.hasPermission(accessControlState, caller, #admin)) {
Runtime.trap("Unauthorized: Only admins can perform this action");
};
// Users only:
if (not AccessControl.hasPermission(accessControlState, caller, #user)) {
Runtime.trap("Unauthorized: Only users can perform this action");
};
// Any user including guests: No check needed为每个公共函数应用合适的权限守卫:
// 仅管理员可访问:
if (not AccessControl.hasPermission(accessControlState, caller, #admin)) {
Runtime.trap("Unauthorized: Only admins can perform this action");
};
// 仅普通用户可访问:
if (not AccessControl.hasPermission(accessControlState, caller, #user)) {
Runtime.trap("Unauthorized: Only users can perform this action");
};
// 包括访客在内的所有用户:无需检查Design Guidelines
设计准则
- Anonymous principals are treated as guests.
- includes an admin-only guard internally.
assignRole - Use for authenticated endpoints that modify data.
shared({ caller }) - Use for authenticated endpoints that fetch data.
query({ caller }) - Handle ownership verification where needed.
- Use for authorization failures.
Runtime.trap
- 匿名主体(Principal)被视为访客。
- 内部包含仅管理员可调用的守卫。
assignRole - 对于修改数据的认证端点,使用。
shared({ caller }) - 对于获取数据的认证端点,使用。
query({ caller }) - 必要时处理所有权验证。
- 授权失败时使用抛出错误。
Runtime.trap
Email Attributes
邮箱属性
MixinAuthorizationnullDo NOT use the mixin directly -- always go through . The callback receives the caller principal and the verified attributes:
mo:identity-attributesMixinAuthorization{
name : ?Text; // verified display name, when present
email : ?Text; // always the verified address -- sourced from II's `verified_email`, never the unverified `email` key
sso : ?Text; // SSO domain when the identity came from SSO, otherwise null
}The field is named , but it only ever holds II's value -- the unverified key is never read. Use in the callback (there is no field).
emailverified_emailemailattrs.emailattrs.verified_emailStore them in your own state and expose a getter to read them back:
import Map "mo:core/Map";
import Principal "mo:core/Principal";
import AccessControl "mo:caffeineai-authorization/access-control";
import MixinAuthorization "mo:caffeineai-authorization/MixinAuthorization";
actor {
let accessControlState = AccessControl.initState();
let emails = Map.empty<Principal, Text>();
include MixinAuthorization(
accessControlState,
?(func(caller : Principal, attrs : { name : ?Text; email : ?Text; sso : ?Text }) {
switch (attrs.email) {
case (?email) { emails.add(caller, email) };
case null {};
};
}),
);
public query ({ caller }) func getCallerEmail() : async ?Text {
emails.get(caller);
};
};The and canister environment variables required for attribute verification are configured automatically by the Caffeine platform -- you do not set them.
trusted_attribute_signersfrontend_originsMixinAuthorizationnull请勿直接使用混入类——务必通过调用。回调函数会收到调用者主体和已验证的属性:
mo:identity-attributesMixinAuthorization{
name : ?Text; // 已验证的显示名称(如有)
email : ?Text; // 始终为已验证的地址——来自II的`verified_email`,绝不会读取未验证的`email`字段
sso : ?Text; // 当身份来自SSO时的SSO域名,否则为null
}字段名为,但仅存储II的值——未验证的字段绝不会被读取。在回调中使用(不存在字段)。
emailverified_emailemailattrs.emailattrs.verified_email将这些属性存储在自定义状态中,并暴露一个获取方法:
import Map "mo:core/Map";
import Principal "mo:core/Principal";
import AccessControl "mo:caffeineai-authorization/access-control";
import MixinAuthorization "mo:caffeineai-authorization/MixinAuthorization";
actor {
let accessControlState = AccessControl.initState();
let emails = Map.empty<Principal, Text>();
include MixinAuthorization(
accessControlState,
?(func(caller : Principal, attrs : { name : ?Text; email : ?Text; sso : ?Text }) {
switch (attrs.email) {
case (?email) { emails.add(caller, email) };
case null {};
};
}),
);
public query ({ caller }) func getCallerEmail() : async ?Text {
emails.get(caller);
};
}属性验证所需的和容器环境变量由Caffeine平台自动配置——无需手动设置。
trusted_attribute_signersfrontend_originsFetching the Email on the Frontend
在前端获取邮箱
After sign-in, query the getter like any other authenticated actor method:
typescript
const { data: callerEmail } = useQuery<string | null>({
queryKey: ['callerEmail'],
queryFn: () => actor.getCallerEmail(),
enabled: !!actor && isAuthenticated,
});登录后,像调用其他认证后的actor方法一样调用获取方法:
typescript
const { data: callerEmail } = useQuery<string | null>({
queryKey: ['callerEmail'],
queryFn: () => actor.getCallerEmail(),
enabled: !!actor && isAuthenticated,
});Frontend
前端
Authentication system with role-based access control.
带有基于角色访问控制的身份认证系统。
User Profile Setup
用户配置文件设置
When using Internet Identity, the user gets a principal id only after login. Anonymous principals are treated as guests. The principal id is not human-readable -- ask the user for their name the first time they log in with a new principal.
Backend API for profiles:
- -- returns
getCallerUserProfile(): Promise<UserProfile | null>if no profile existsnull - -- saves name and profile data
saveCallerUserProfile(profile: UserProfile): Promise<void> - -- fetch another user's profile
getUserProfile(user: Principal): Promise<UserProfile | null>
Rules:
- On login, if the user already has a profile, do not ask for the name again
- Display the user's profile name instead of the principal id
- Make sure the user must be logged in before seeing any application data
- When logging out, clear all cached application data including the cached user profile
使用Internet Identity时,用户仅在登录后获得主体ID(principal id)。匿名主体被视为访客。主体ID不具备可读性——用户首次使用新主体登录时,请询问其姓名。
配置文件相关后端API:
- —— 若配置文件不存在则返回
getCallerUserProfile(): Promise<UserProfile | null>null - —— 保存姓名和配置文件数据
saveCallerUserProfile(profile: UserProfile): Promise<void> - —— 获取其他用户的配置文件
getUserProfile(user: Principal): Promise<UserProfile | null>
规则:
- 登录时,若用户已有配置文件,则不再询问姓名
- 显示用户的配置文件姓名而非主体ID
- 确保用户必须登录后才能查看任何应用数据
- 登出时,清除所有缓存的应用数据,包括缓存的用户配置文件
Preventing Profile Setup Modal Flash
避免配置文件设置弹窗闪现
typescript
export function useGetCallerUserProfile() {
const { actor, isFetching: actorFetching } = useActor();
const query = useQuery<UserProfile | null>({
queryKey: ['currentUserProfile'],
queryFn: async () => {
if (!actor) throw new Error('Actor not available');
return actor.getCallerUserProfile();
},
enabled: !!actor && !actorFetching,
retry: false,
});
return {
...query,
isLoading: actorFetching || query.isLoading,
isFetched: !!actor && query.isFetched,
};
}Then in your component:
typescript
const showProfileSetup = isAuthenticated && !profileLoading && isFetched && userProfile === null;typescript
export function useGetCallerUserProfile() {
const { actor, isFetching: actorFetching } = useActor();
const query = useQuery<UserProfile | null>({
queryKey: ['currentUserProfile'],
queryFn: async () => {
if (!actor) throw new Error('Actor not available');
return actor.getCallerUserProfile();
},
enabled: !!actor && !actorFetching,
retry: false,
});
return {
...query,
isLoading: actorFetching || query.isLoading,
isFetched: !!actor && query.isFetched,
};
}然后在组件中:
typescript
const showProfileSetup = isAuthenticated && !profileLoading && isFetched && userProfile === null;Auth State Lifecycle
认证状态生命周期
The hook exposes two kinds of state — use the right one:
useInternetIdentity| Scenario | | |
|---|---|---|
| Page load, no stored session | | |
| Page load, restoring stored session | | |
| Stored session restored after reload | | |
| Interactive login in progress (popup open) | | |
| Interactive login just completed | | |
| Login popup failed / cancelled | | |
IMPORTANT: () is only after an interactive login via the popup. It is NOT when a stored identity is restored on page reload. Never use to gate authenticated vs. unauthenticated UI — always use .
isLoginSuccessloginStatus === "success"truetrueisLoginSuccessisAuthenticatedKey states for the login button:
- —
isInitializingis loading from IndexedDB; disable the button to prevent clicks before the client is ready.AuthClient - — the II popup is open; disable the button to prevent duplicate popups.
isLoggingIn
useInternetIdentity| 场景 | | |
|---|---|---|
| 页面加载,无存储会话 | | |
| 页面加载,恢复存储会话 | | |
| 页面重载后恢复存储会话 | | |
| 交互登录进行中(弹窗打开) | | |
| 交互登录刚完成 | | |
| 登录弹窗失败/取消 | | |
重要提示: (即)仅在通过弹窗完成交互登录后为。页面重载恢复存储身份时,该值不会为。切勿使用来区分认证与未认证UI——请始终使用。
isLoginSuccessloginStatus === "success"truetrueisLoginSuccessisAuthenticated登录按钮的关键状态:
- ——
isInitializing正在从IndexedDB加载;禁用按钮以防止客户端准备好前被点击。AuthClient - —— II弹窗已打开;禁用按钮以防止重复弹窗。
isLoggingIn
Login Component
登录组件
typescript
import { useInternetIdentity } from '@caffeineai/core-infrastructure';
import { useQueryClient } from '@tanstack/react-query';
export default function LoginButton() {
const { login, clear, isAuthenticated, isInitializing, isLoggingIn } = useInternetIdentity();
const queryClient = useQueryClient();
const handleAuth = () => {
if (isAuthenticated) {
clear();
queryClient.clear();
} else {
login();
}
};
return (
<button
onClick={handleAuth}
disabled={isInitializing || isLoggingIn}
className={`px-6 py-2 rounded-full transition-colors font-medium ${
isAuthenticated
? 'bg-gray-200 hover:bg-gray-300 text-gray-800'
: 'bg-blue-600 hover:bg-blue-700 text-white'
} disabled:opacity-50`}
>
{isInitializing ? 'Loading...' : isAuthenticated ? 'Logout' : 'Login'}
</button>
);
}The and functions are fire-and-forget (they don't return promises that track the full flow). The hook's / states track the async lifecycle — do not wrap them in local / logic.
login()clear()isLoggingInisInitializinguseStateisPendingGate authenticated UI on (covers both fresh login and restored sessions on page reload):
isAuthenticatedtypescript
{isAuthenticated ? (
<AuthenticatedApp />
) : (
<LoginScreen />
)}typescript
import { useInternetIdentity } from '@caffeineai/core-infrastructure';
import { useQueryClient } from '@tanstack/react-query';
export default function LoginButton() {
const { login, clear, isAuthenticated, isInitializing, isLoggingIn } = useInternetIdentity();
const queryClient = useQueryClient();
const handleAuth = () => {
if (isAuthenticated) {
clear();
queryClient.clear();
} else {
login();
}
};
return (
<button
onClick={handleAuth}
disabled={isInitializing || isLoggingIn}
className={`px-6 py-2 rounded-full transition-colors font-medium ${
isAuthenticated
? 'bg-gray-200 hover:bg-gray-300 text-gray-800'
: 'bg-blue-600 hover:bg-blue-700 text-white'
} disabled:opacity-50`}
>
{isInitializing ? 'Loading...' : isAuthenticated ? 'Logout' : 'Login'}
</button>
);
}login()clear()isLoggingInisInitializinguseStateisPending通过控制认证UI的显示(涵盖新登录和页面重载恢复会话两种情况):
isAuthenticatedtypescript
{isAuthenticated ? (
<AuthenticatedApp />
) : (
<LoginScreen />
)}Comparing Current User with Data Author
对比当前用户与数据作者
typescript
import { useInternetIdentity } from '@caffeineai/core-infrastructure';
import type { Principal } from '@icp-sdk/core/principal';
const { identity } = useInternetIdentity();
const isAuthor = (authorPrincipal: Principal): boolean => {
if (!identity) return false;
return authorPrincipal.toString() === identity.getPrincipal().toString();
};typescript
import { useInternetIdentity } from '@caffeineai/core-infrastructure';
import type { Principal } from '@icp-sdk/core/principal';
const { identity } = useInternetIdentity();
const isAuthor = (authorPrincipal: Principal): boolean => {
if (!identity) return false;
return authorPrincipal.toString() === identity.getPrincipal().toString();
};Access Control UI
访问控制UI
For admin-only or personal applications, show an AccessDeniedScreen component when unauthorized users try to access the application.
对于仅管理员或个人使用的应用,当未授权用户尝试访问时,显示AccessDeniedScreen组件。
Error Handling
错误处理
Handle authorization errors from backend calls gracefully in the UI with appropriate error messages shown to the user.
Debug.trapNote: The initialization of the first admin is done automatically in . The first authenticated user to log in becomes admin; no token or secret is needed.
@caffeineai/core-infrastructure在UI中优雅处理后端调用抛出的授权错误,向用户显示合适的错误信息。
Debug.trap注意:第一个管理员的初始化在中自动完成。第一个完成身份认证的用户将成为管理员,无需令牌或密钥。
@caffeineai/core-infrastructure