前言

最近在寻找一个多端同步的两步认证应用,于是发现了2FAuth,此应用可以使用docker搭建,且支持PWA安装,填补了我在PC端2FA认证的空缺

准备

首选你需要一台vps,或者也可以本地搭建(但本地搭建也就无法满足多端同步)
然后你还需要准备一个域名

服务器配置建议:实测ram占用1m,cpu占用几乎为零加上dockerd占用100m多,所以弱鸡也可以跑

安装

配置文件

这是官方给的docker-compose文件

version: "3"
services:
  2fauth:
    image: 2fauth/2fauth
    container_name: 2fauth
    volumes:
      - ./2fauth:/2fauth
    ports:
      - 8000:8000/tcp
    environment:
      # You can change the name of the app
      - APP_NAME=2FAuth
      # You can leave this on "local". If you change it to production most console commands will ask for extra confirmation.
      # Never set it to "testing".
      - APP_ENV=local
      # Set to true if you want to see debug information in error screens.
      - APP_DEBUG=false
      # This should be your email address
      - SITE_OWNER=mail@example.com
      # The encryption key for  our database and sessions. Keep this very secure.
      # If you generate a new one all existing data must be considered LOST.
      # Change it to a string of exactly 32 chars or use command `php artisan key:generate` to generate it
      - APP_KEY=SomeRandomStringOf32CharsExactly
      # This variable must match your installation's external address.
      # Webauthn won't work otherwise.
      - APP_URL=http://localhost
      # If you want to serve js assets from a CDN (like https://cdn.example.com),
      # you need to set this custom URL here.
      # Otherwise, this should be the exact same value as APP_URL.
      - ASSET_URL=http://localhost
      # Turn this to true if you want your app to react like a demo.
      # The Demo mode reset the app content every hours and set a generic demo user.
      - IS_DEMO_APP=false
      # The log channel defines where your log entries go to.
      # 'daily' is the default logging mode giving you 7 daily rotated log files in /storage/logs/.
      # Also available are 'errorlog', 'syslog', 'stderr', 'papertrail', 'slack' and a 'stack' channel
      # to combine multiple channels into a single one.
      - LOG_CHANNEL=daily
      # Log level. You can set this from least severe to most severe:
      # debug, info, notice, warning, error, critical, alert, emergency
      # If you set it to debug your logs will grow large, and fast. If you set it to emergency probably
      # nothing will get logged, ever.
      - LOG_LEVEL=notice
      # Database config (can only be sqlite)
      - DB_DATABASE="/srv/database/database.sqlite"
      # If you're looking for performance improvements, you could install memcached.
      - CACHE_DRIVER=file
      - SESSION_DRIVER=file
      # Mail settings
      # Refer your email provider documentation to configure your mail settings
      # Set a value for every available setting to avoid issue
      - MAIL_DRIVER=log
      - MAIL_HOST=smtp.mailtrap.io
      - MAIL_PORT=2525
      - MAIL_USERNAME=null
      - MAIL_PASSWORD=null
      - MAIL_ENCRYPTION=null
      - MAIL_FROM_NAME=null
      - MAIL_FROM_ADDRESS=null
      # SSL peer verification.
      # Set this to false to disable the SSL certificate validation.
      # WARNING
      # Disabling peer verification can result in a major security flaw.
      # Change it only if you know what you're doing.
      - MAIL_VERIFY_SSL_PEER=true
      # API settings
      # The maximum number of API calls in a minute from the same IP.
      # Once reached, all requests from this IP will be rejected until the minute has elapsed.
      # Set to null to disable the API throttling.
      - THROTTLE_API=60
      # Authentication settings
      # The number of times per minute a user can fail to log in before being locked out.
      # Once reached, all login attempts will be rejected until the minute has elapsed.
      # This setting applies to both email/password and webauthn login attemps.
      - LOGIN_THROTTLE=5
      # The default authentication guard
      # Supported:
      #   'web-guard' : The Laravel built-in auth system (default if nulled)
      #   'reverse-proxy-guard' : When 2FAuth is deployed behind a reverse-proxy that handle authentication
      # WARNING
      # When using 'reverse-proxy-guard' 2FAuth only look for the dedicated headers and skip all other built-in
      # authentication checks. That means your proxy is fully responsible of the authentication process, 2FAuth will
      # trust him as long as headers are presents.
      - AUTHENTICATION_GUARD=web-guard
      # Name of the HTTP headers sent by the reverse proxy that identifies the authenticated user at proxy level.
      # Check your proxy documentation to find out how these headers are named (i.e 'REMOTE_USER', 'REMOTE_EMAIL', etc...)
      # (only relevant when AUTHENTICATION_GUARD is set to 'reverse-proxy-guard')
      - AUTH_PROXY_HEADER_FOR_USER=null
      - AUTH_PROXY_HEADER_FOR_EMAIL=null
      # Custom logout URL to open when using an auth proxy.
      - PROXY_LOGOUT_URL=null
      # WebAuthn settings
      # Relying Party name, aka the name of the application. If null, defaults to APP_NAME
      - WEBAUTHN_NAME=2FAuth
      # Relying Party ID. If null, the device will fill it internally.
      # See https://webauthn-doc.spomky-labs.com/prerequisites/the-relying-party#how-to-determine-the-relying-party-id
      - WEBAUTHN_ID=null
      # Optional image data in BASE64 (128 bytes maximum) or an image url
      # See https://webauthn-doc.spomky-labs.com/prerequisites/the-relying-party#relying-party-icon
      - WEBAUTHN_ICON=null
      # Use this setting to control how user verification behave during the
      # WebAuthn authentication flow.
      #
      # Most authenticators and smartphones will ask the user to actively verify
      # themselves for log in. For example, through a touch plus pin code,
      # password entry, or biometric recognition (e.g., presenting a fingerprint).
      # The intent is to distinguish one user from any other.
      #
      # Supported:
      #   'required': Will ALWAYS ask for user verification
      #   'preferred' (default) : Will ask for user verification IF POSSIBLE
      #   'discouraged' : Will NOT ask for user verification (for example, to minimize disruption to the user interaction flow)
      - WEBAUTHN_USER_VERIFICATION=preferred
      #### SSO settings (for Socialite) ####
      # Uncomment and complete lines for the OAuth providers you want to enable.
      # - OPENID_AUTHORIZE_URL=
      # - OPENID_TOKEN_URL=
      # - OPENID_USERINFO_URL=
      # - OPENID_CLIENT_ID=
      # - OPENID_CLIENT_SECRET=
      # - GITHUB_CLIENT_ID=
      # - GITHUB_CLIENT_SECRET=
      # Use this setting to declare trusted proxied.
      # Supported:
      #   '*': to trust any proxy
      #   A comma separated IP list: The list of proxies IP to trust
      - TRUSTED_PROXIES=null
      # Proxy for outgoing requests like new releases detection or logo fetching.
      # You can provide a proxy URL that contains a scheme, username, and password.
      # For example, "http://username:password@192.168.16.1:10".
      - PROXY_FOR_OUTGOING_REQUESTS=null
      # Leave the following configuration vars as is.
      # Unless you like to tinker and know what you're doing.
      - BROADCAST_DRIVER=log
      - QUEUE_DRIVER=sync
      - SESSION_LIFETIME=120
      - REDIS_HOST=127.0.0.1
      - REDIS_PASSWORD=null
      - REDIS_PORT=6379
      - PUSHER_APP_ID=
      - PUSHER_APP_KEY=
      - PUSHER_APP_SECRET=
      - PUSHER_APP_CLUSTER=mt1
      - VITE_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
      - VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
      - MIX_ENV=local

但官方的配置过多,如果只是个人使用,不提供给他人使用,可以按照我的配置来写,我还加入了自己的部分配置

version: "3"
services:
  2fauth:
    image: 2fauth/2fauth
    container_name: 2fauth
    restart: always
    volumes:
      - ./data:/2fauth
    networks:
      2fauth:
        # 设置容器ip,方便后续反向代理
        ipv4_address: 172.20.0.2
    environment:
      - APP_NAME=orii2FAuth
        # 设置一个32位的app_key,可以使用it-tool生成https://it-tools.tech/token-generator
      - APP_KEY=SomeRandomStringOf32CharsExactly
        # 设置你的域名
      - APP_URL=https://auth.example.com
      - IS_DEMO_APP=false
      - LOG_CHANNEL=daily
      - LOG_LEVEL=error
      - DB_DATABASE="/srv/database/database.sqlite"
      - CACHE_DRIVER=file
      - SESSION_DRIVER=file
      - AUTHENTICATION_GUARD=web-guard

networks:
  2fauth:
    driver: bridge
    ipam:
      config:
        # 这里根据自己的情况设置,不要和其他容器冲突
        - subnet: 172.20.0.0/16

创建容器

连接到服务器,然后使用如下指令创建目录

mkdir -p 2fauth/data

然后cd进入2fauth目录,使用你常用的文本编辑器把配置文件写入 docker-compose.yaml
接着使用 docker-compose up -d 启动容器

注意,如果你使用的不是root用户,请务必手动创建 data 文件夹,否则会出现权限错误的问题

反向代理

这里我使用的1panel创建的反向代理,你也可以使用熟悉的方式创建
打开 网站 选项,点击 创建网站 ,选择 反向代理 ,按照如图设置

主域名设置为你自己的域名,且与docker-compose配置文件中的要对应
代理地址选择http,然后输入你容器的ip:8000
设置完成后可继续配置https

这里我设置了cloudflare的源证书,并使用cloudflare代理

使用

基本设置

进入你设置的域名 https://auth.example.com ,第一次使用需要注册账号,点击注册
输入信息后登录,然后导入你的配置,可以从aegis,google authenticator等导入
导入后进入设置,将 禁止注册 勾选上

pwa安装

目前支持pwa的主流浏览器有chrome,firefox,safri系列

进入网页后,在链接地址后面会出现此图标,点击即可安装

安卓:使用chrome打开你的网站,点击右上角三个点
然后点击 安装应用 ,安装过程会连接到play商店,需要翻墙(能访问我博客的人应该都具备条件吧)

文章作者: orii
本文链接:
版权声明: 本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 oriiのBlog
Web Web 2FA
喜欢就支持一下吧
打赏
微信 微信
支付宝 支付宝