Authentik 身份验证服务(单点登录)

NOTE

Authentik是一个开源的身份认证和授权服务,可以实现单点登录。支持多种认证方式,包括LDAP,SAML,OIDC,OAuth2等。相较老牌的Keycloak,Authentik更易于部署和维护。

基础步骤

下载最新 docker-compose.yml 文件

wget https://goauthentik.io/docker-compose.yml
  1. 为避免后续升级迁移,更改 postgresql 版本为 15,原版为 12。

  2. 若对安全性有进一步要求,在相应位置更改默认的数据库密码。

安装pwgen

:::caution pwgen 需要EPEL源,如果没有安装,可参考以下链接安装。 :::

dnf install -y pwgen

创建密码及秘钥并存入.env 文件

echo "PG_PASS=$(pwgen -s 40 1)" >> .env
echo "AUTHENTIK_SECRET_KEY=$(pwgen -s 50 1)" >> .env

开启错误日志

echo "AUTHENTIK_ERROR_REPORTING__ENABLED=true" >> .env

可选配置

邮箱配置

# SMTP Host Emails are sent to
AUTHENTIK_EMAIL__HOST=
AUTHENTIK_EMAIL__PORT=
# Optionally authenticate (don't add quotation marks to your password)
AUTHENTIK_EMAIL__USERNAME=
AUTHENTIK_EMAIL__PASSWORD=
# Use StartTLS
AUTHENTIK_EMAIL__USE_TLS=false
# Use SSL
AUTHENTIK_EMAIL__USE_SSL=false
AUTHENTIK_EMAIL__TIMEOUT=10
# Email address authentik will send from, should have a correct @domain
AUTHENTIK_EMAIL__FROM=authentik@localhost

配置端口

:::tip Authentik默认使用 9000 端口(HTTP)以及 9443 端口(HTTPS),如果需要修改,可在 .env 文件中添加以下配置。 :::

COMPOSE_PORT_HTTP=80
COMPOSE_PORT_HTTPS=443

启动&更新

  1. 启动 Authentik

    docker-compose pull
    docker-compose up -d
  2. 更新Authentik

    1. 获取最新的 docker-compose.yml 文件

      进入官网首页,或最新版本的发布地址:Authentik Releases

    2. 下载最新的 docker-compose.yml 文件并覆盖

      wget -O docker-compose.yml https://goauthentik.io/version/2023.5/docker-compose.yml

    其中 -O 参数指定了下载的文件名,覆盖原有的同名文件。

    1. 重新执行启动命令

      docker-compose up -d

实现Gitlab的SSO登录

gitlab_rails['omniauth_enabled'] = true
gitlab_rails['omniauth_allow_single_sign_on'] = ['saml']
gitlab_rails['omniauth_sync_email_from_provider'] = 'saml'
gitlab_rails['omniauth_sync_profile_from_provider'] = ['saml']
gitlab_rails['omniauth_sync_profile_attributes'] = ['email']
gitlab_rails['omniauth_auto_sign_in_with_provider'] = 'saml'
gitlab_rails['omniauth_block_auto_created_users'] = false
gitlab_rails['omniauth_auto_link_saml_user'] = true
gitlab_rails['omniauth_providers'] = [
  {
    name: 'saml',
    args: {
      assertion_consumer_service_url: 'https://gitlab.example.com/users/auth/saml/callback',
      # Shown when navigating to certificates in authentik
      idp_cert_fingerprint: '97:07:ee:4a:4c:09:ca:6f:06:39:0c:ee:df:cb:f9:24:71:d5:6c:8f',
      idp_sso_target_url: 'https://sso.example.com/application/saml/gitlab/sso/binding/redirect/',
      issuer: 'https://gitlab.example.com',
      name_identifier_format: 'urn:oasis:names:tc:SAML:2.0:nameid-format:persistent',
      attribute_statements: {
        email: ['http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress'],
        first_name: ['http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name'],
        nickname: ['http://schemas.goauthentik.io/2021/02/saml/username']
      }
    },
    label: 'authentik'
  }
]