ホームアシスタント統合 SSO

他のソフトウェアは nginx をプロキシとして直接使用して認証を追加できますが、この hass はユーザーのセキュリティとプライバシーの点で非常に強力です。プロキシになるにはホワイトリストを構成する必要があり、サポートされているサードパーティ認証は私のニーズには適していませんソース コードを変更する必要があります。OK、後でソース コードを変更せずに妥協点を見つけました。

参考記事

hass をcontainerdにデプロイし、ssoを統合する

それをkubernetesにデプロイしました。リンクを参照してください

ローカルで hass にログインし、有効期間の長いトークンを作成する

ここに画像の説明を挿入

hass設定ファイル

# 配置nginx代理
http:
  use_x_forwarded_for: true
  trusted_proxies:
    - 10.234.104.88 #这个地址是nginx服务的ip地址

nginx設定ファイル

server {
    
    
    listen       86;
    listen  [::]:86;
    server_name  localhost;
	#替换响应中的内容,类似于string.replace函数
    sub_filter '<head>' '<head><script>window.externalApp={getExternalAuth:function(){window.externalAuthSetToken(true,{"access_token":"第一步创建长期的令牌","expires_in":248832000});},revokeExternalAuth:function(){window.externalAuthRevokeToken(false);}};</script>';
    sub_filter_once on;

    location / {
    
    
        proxy_pass http://localhost:8123;#hass地址
        auth_request http://xxx/sso/auth;#sso鉴权接口地址
        proxy_set_header Host $host;
        proxy_set_header Accept-Encoding "";
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $http_connection;
        proxy_set_header X-Real-IP $remote_addr; # //一层代理时是用户真实ip,二层代理时是第一台nginxip
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # //一层代理时没有值,多层代理里面会存储多个ip值,第一个值就是真实用户ip
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    location /api/websocket {
    
    
        proxy_pass http://localhost:8123/api/websocket;
        auth_request http://xxx/sso/auth;#sso鉴权接口地址
        proxy_set_header Host $host;
        proxy_set_header Accept-Encoding "";
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $http_connection;
        proxy_set_header X-Real-IP $remote_addr; # //一层代理时是用户真实ip,二层代理时是第一台nginxip
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # //一层代理时没有值,多层代理里面会存储多个ip值,第一个值就是真实用户ip
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    location /auth/authorize {
    
    
        # tell nginx that this request requires authentication with oauth2_proxy
        auth_request http://xxx/sso/auth;#sso鉴权接口地址
        # redirect back to the frontend and tell it to use external_auth
        return 301 /?external_auth=1;
    }
}

フォーマットします:

<script>
    window.externalApp = {
    
    
        getExternalAuth: function() {
    
    
            window.externalAuthSetToken(true, {
    
    
                "access_token": "第一步创建长期的令牌",
                "expires_in": 248832000
            });
        },
        revokeExternalAuth: function() {
    
    
            window.externalAuthRevokeToken(false);
        }
    };
</script>

実装プロセス

  1. ユーザーはnginxのポート86にアクセスします
  2. nginx は、まず認証のためにアドレス http://xxx/sso/auth を要求し、応答コードが 200 の場合、要求を hass アドレス http://localhost:8123 に転送します。
  3. 認証に失敗し、401などの不正なレスポンスコードが返された場合、nginxはブラウザに直接401エラーを返し、hassへのアクセスが禁止されるため、先にssoでログインして認証を行ってください。
  4. ここで、nginx は、has をプロキシするときに応答のコンテンツを置き換え、sub_filterそれにコンテンツを追加<head><script>window.externalApp.......します。これは、hass に外部認可認証を実行するように指示するためです。getExternalAuth 関数を実行してトークンを取得し、ここで長期トークンを直接返します。 。nginx で認証を行ったので、ここでは認証を行いません。

おすすめ

転載: blog.csdn.net/weixin_48835367/article/details/131972193