Immich 的 OAuth 身份验证

Immich 的 OAuth 身份验证

本页介绍如何在Immich中使用OAuth。

提示

如果无法把app.immich:///oauth-callback设置为有效重定向URI,参见“移动端重定向URI”中的替代方案。

概览

Immich通过OpenID Connect(OIDC)支持第三方身份验证。OIDC是在OAuth2之上构建的身份层,大多数身份提供商都支持它,包括:

前置条件

在Immich中启用OAuth前,需要先在第三方身份验证服务器中配置一个客户端应用。不同提供商的具体操作不同,但总体方法相同。

  1. 创建新的客户端应用

    1. Provider类型应为OpenID Connect或OAuth2。
    2. Client类型应为Confidential。
    3. Application类型应为Web。
    4. Grant类型应为Authorization Code。
  2. 配置重定向URI与来源

    登录重定向URI应包含:

    • app.immich:///oauth-callback:移动应用中的OAuth登录。
    • http://DOMAIN:PORT/auth/login:Web客户端中的OAuth登录。
    • http://DOMAIN:PORT/user-settings:在Web客户端中手动关联OAuth。

    重定向URI应覆盖访问Immich时会使用的所有域名。例如:

    移动端

    • app.immich:///oauth-callback:必须包含此项,iOS与Android应用才能正常工作。

    本机地址

    • http://localhost:2283/auth/login
    • http://localhost:2283/user-settings

    本地IP地址

    • http://192.168.0.200:2283/auth/login
    • http://192.168.0.200:2283/user-settings

    主机名

    • https://immich.example.com/auth/login
    • https://immich.example.com/user-settings
  3. 配置后端通道注销URL

    如果身份验证服务器支持,可以指定Backchannel logout URL,格式为http://DOMAIN:PORT/api/oauth/backchannel-logout。

启用 OAuth

配置好OAuth客户端应用后,在Immich Web端的Administration -> Settings页面中配置OAuth。

设置项 类型 默认值 说明
启用 布尔值 false 启用或禁用OAuth。
issuer_url URL (必填) 必填:客户端的自动发现URL,来自上一步。
client_id 字符串 (必填) 必填:上一步获得的Client ID。
client_secret 字符串 (必填) 必填:上一步获得的Client Secret。
scope 字符串 openid email profile 随请求发送的完整scope列表,以空格分隔。
id_token_signed_response_alg 字符串 RS256 ID令牌的签名算法,例如RS256、HS256。
userinfo_signed_response_alg 字符串 none userinfo响应的签名算法,例如RS256、HS256。
prompt 字符串 (空) 授权URL的prompt参数,例如select_account、login、consent。
end_session_endpoint URL (空) HTTP(S)形式的替代会话结束端点,即注销URI。
Request timeout(请求超时) 字符串 30,000(30秒) 放弃HTTP请求前等待的毫秒数。
Storage Label Claim(存储标签声明) 字符串 preferred_username 用户存储标签的claim映射。¹
Role Claim(角色声明) 字符串 immich_role 用户角色的claim映射,应返回"user"或"admin"。¹
Storage Quota Claim(存储配额声明) 字符串 immich_quota 用户存储配额的claim映射。¹
Default Storage Quota(默认存储配额,GiB) 数值 0 没有存储配额claim时采用的默认配额,留空表示不限额。
Button Text(按钮文字) 字符串 Login with OAuth Web端OAuth按钮的文字。
Auto Register(自动注册) 布尔值 true 为true时,用户首次登录将自动注册。
Auto Launch(自动启动) 布尔值 false 为true时,跳过登录页并自动启动OAuth登录流程。
移动端重定向 URI 覆盖(覆盖移动端重定向URI) URL (空) HTTP(S)形式的替代移动端重定向URI。

Claim Options [1]

这些claim只在创建用户时使用,之后不会同步。

信息

Issuer URL应类似以下形式,并返回有效的JSON文档。

  • https://accounts.google.com/.well-known/openid-configuration
  • http://localhost:9000/application/o/immich/.well-known/openid-configuration

URL中的.well-known/openid-configuration部分可以省略,发现过程会自动添加。

自动启动登录

启用Auto Launch后,登录页会自动跳转到OAuth授权URL。要重新打开登录界面,可使用浏览器的后退按钮,或直接访问/auth/login?autoLaunch=0。也可以通过/auth/login?autoLaunch=1按请求启用Auto Launch。例如,Nextcloud通过External sites与oidc应用调用Immich时,这可以让用户直接使用已登录的Immich实例。

移动端重定向 URI

移动应用的重定向URI为app.immich:///oauth-callback,使用自定义scheme。如果OAuth提供商不接受这种URI,可以按以下步骤处理:

  1. 配置一个HTTP(S)端点,把请求转发到app.immich:///oauth-callback。
  2. 在身份提供商中,将新端点加入有效重定向URI白名单。
  3. 在Immich的OAuth设置中,把新端点填入Mobile Redirect URI Override。

完成后,移动应用就可以使用OAuth,而无需让身份提供商接受自定义scheme的重定向URI。

信息

Immich已经提供/api/oauth/mobile-redirect路由,会转发到app.immich:///oauth-callback,可用于第一步。

配置示例

Authelia Example

Authelia 示例

以下示例展示如何配置Authelia OAuth:

示例假定用户schema中存在immichquota属性,用来设置Immich存储配额。与配额相关的配置是可选的。

authentication_backend:
  ldap:
    # The LDAP server configuration goes here.
    # See: https://www.authelia.com/c/ldap
    attributes:
      extra:
        immichquota: # The attribute name from LDAP
          name: 'immich_quota'
          multi_valued: false
          value_type: 'integer'
identity_providers:
  oidc:
    ## The other portions of the mandatory OpenID Connect 1.0 configuration go here.
    ## See: https://www.authelia.com/c/oidc
    claims_policies:
      immich_policy:
        custom_claims:
          immich_quota:
            attribute: 'immich_quota'
    scopes:
      immich_scope:
        claims:
          - 'immich_quota'

    clients:
      - client_id: 'immich'
        client_name: 'Immich'
        # https://www.authelia.com/integration/openid-connect/frequently-asked-questions/#how-do-i-generate-a-client-identifier-or-client-secret
        client_secret: '$pbkdf2-sha512$310000$c8p78n7pUMln0jzvd4aK4Q$JNRBzwAo0ek5qKn50cFzzvE9RXV88h1wJn5KGiHrD0YKtZaR/nCb2CJPOsKaPK0hjf.9yHxzQGZziziccp6Yng'
        public: false
        require_pkce: true
        pkce_challenge_method: 'S256'
        redirect_uris:
          - 'https://example.immich.app/auth/login'
          - 'https://example.immich.app/user-settings'
          - 'app.immich:///oauth-callback'
        scopes:
          - 'openid'
          - 'profile'
          - 'email'
          - 'immich_scope'
        claims_policy: 'immich_policy'
        response_types:
          - 'code'
        grant_types:
          - 'authorization_code'
        id_token_signed_response_alg: 'RS256'
        userinfo_signed_response_alg: 'RS256'
        token_endpoint_auth_method: 'client_secret_post'

Immich系统设置中的OAuth配置

设置项 值
Issuer URL(签发者URL) https://auth.example.com
Client ID(客户端标识) immich
Client Secret(客户端密钥) 0v89FXkQOWO**************mprbvXD549HH6s1iw…
Token Endpoint Auth Method(令牌端点认证方法) client_secret_post
Scope(权限范围) openid email profile immich_scope
ID Token Signed Response Algorithm(ID令牌签名算法) RS256
Userinfo Signed Response Algorithm(用户信息响应签名算法) RS256
End Session Endpoint(会话结束端点) https://auth.example.com/logout?rd=https://immich.example.com/
Storage Label Claim(存储标签声明) uid
Storage Quota Claim(存储配额声明) immich_quota
Default Storage Quota(默认存储配额,GiB) 0(留空表示不限额)
Button Text(按钮文字) Sign in with Authelia(可选)
Auto Register(自动注册) 启用(可选)
Auto Launch(自动启动) 启用(可选)
Mobile Redirect URI Override(覆盖移动端重定向URI) 禁用
Mobile Redirect URI(移动端重定向URI)
Authentik Example

Authentik 示例

以下示例展示如何配置Authentik OAuth:

Authentik OAuth2/OpenID Provider中的授权重定向URI配置

图片[1]-Immich 的 OAuth 身份验证-未完纪

Immich系统设置中的OAuth配置

设置项 值
Issuer URL(签发者URL) https://authentik.example.com/application/o/immich/
Client ID(客户端标识) AFCj2rM1f4rps*************lCLEum6hH9…
Client Secret(客户端密钥) 0v89FXkQOWO**************mprbvXD549HH6s1iw…
Scope(权限范围) openid email profile
Signing Algorithm(签名算法) RS256
Storage Label Claim(存储标签声明) preferred_username
Storage Quota Claim(存储配额声明) immich_quota
Default Storage Quota(默认存储配额,GiB) 0(留空表示不限额)
Button Text(按钮文字) Sign in with Authentik(可选)
Auto Register(自动注册) 启用(可选)
Auto Launch(自动启动) 启用(可选)
Mobile Redirect URI Override(覆盖移动端重定向URI) 禁用
Mobile Redirect URI(移动端重定向URI)
Google Example

Google 示例

以下示例展示如何配置Google OAuth:

Google控制台中的授权重定向URI配置

图片[2]-Immich 的 OAuth 身份验证-未完纪

Immich系统设置中的OAuth配置

设置项 值
Issuer URL(签发者URL) https://accounts.google.com
Client ID(客户端标识) 7******************vuls.apps.googleusercontent.com
Client Secret(客户端密钥) G******************OO
Scope(权限范围) openid email profile
Signing Algorithm(签名算法) RS256
Storage Label Claim(存储标签声明) preferred_username
Storage Quota Claim(存储配额声明) immich_quota
Default Storage Quota(默认存储配额,GiB) 0(留空表示不限额)
Button Text(按钮文字) Sign in with Google(可选)
Auto Register(自动注册) 启用(可选)
Auto Launch(自动启动) 启用
Mobile Redirect URI Override(覆盖移动端重定向URI) 启用(必需)
Mobile Redirect URI(移动端重定向URI) https://example.immich.app/api/oauth/mobile-redirect
Keycloak Example

Keycloak 示例

以下示例展示如何配置Keycloak OAuth:

在Keycloak Realm中创建immich客户端。

图片[3]-Immich 的 OAuth 身份验证-未完纪图片[4]-Immich 的 OAuth 身份验证-未完纪图片[5]-Immich 的 OAuth 身份验证-未完纪

Immich系统设置中的OAuth配置

设置项 值
Issuer URL(签发者URL) https://<KEYCLOAK_DOMAIN>/realms/<YOUR_REALM>
Client ID(客户端标识) immich
Client Secret(客户端密钥) 可从Clients -> immich -> Credentials获取。
Scope(权限范围) openid email profile
Signing Algorithm(签名算法) RS256
Storage Label Claim(存储标签声明) preferred_username
Role Claim(角色声明) immich_role
Storage Quota Claim(存储配额声明) immich_quota
Default Storage Quota(默认存储配额,GiB) 0(留空表示不限额)
Button Text(按钮文字) Sign in with Keycloak(推荐)
Auto Register(自动注册) 启用(可选)
Auto Launch(自动启动) 启用(可选)
Mobile Redirect URI Override(覆盖移动端重定向URI) 禁用
Mobile Redirect URI(移动端重定向URI)

Role Claim可通过Client Role管理。记得创建claim名称为immich_role的映射器。

正文相关链接

来源与许可

原文:OAuth Authentication。本页为中文翻译与排版整理,示例源码保留原样,权利归原作者及维护者所有。

Immich 官方文档。原站注明 Immich 软件按 GNU AGPL v3 提供;此处不将软件许可擅自扩展为全部第三方图片的许可。

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容