使用Grafana API的自动身份验证

N'B*_*yev 3 authentication api get grafana grafana-api

在我的Web应用程序中,我想提供将经过身份验证的用户从仪表板传递到Grafana的功能

用户使用凭据登录我的仪表板后,将在我的应用程序中显示指向Grafana仪表板的链接。当用户单击该链接时,他/她将被重定向到Grafana页面并自动登录,而不会显示Grafana登录页面。我不希望我的用户必须遇到第二个登录屏幕,在该屏幕上他们会迷惑要输入的用户名/密码。

我遵循了从Web应用程序自动登录到grafana自动登录到grafana仪表板使用凭据或令牌从Web应用程序自动登录到grafana 通过令牌url自动登录的方法,但是没有运气。我找不到合适的清洁解决方案。

我正在使用在Ubuntu Server 18.04上安装的Grafana v6.2.5。

我该如何实施?任何帮助,将不胜感激。

服务器详细信息:Ubuntu Server 18.04,Apache 2.4.29

N'B*_*yev 5

经过一番挖掘后,我发现了使用Grafana的通用OAuth身份验证的解决方法。

步骤1:使用以下代码创建文件。

GrafanaOAuth.php

<?php
    declare(strict_types=1);

    class GrafanaOAuth {
        protected $user;

        /**
         * Create a new GrafanaOAuth instance.
         * @param array $user
         * @return void
         */
        public function __construct(array $user) {
            $this->user = $user;
        }

        /**
         * Redirect to authentication URL.
         * @param string $state
         * @return void
         */
        public function auth(string $state): void {
            $state = urlencode($state);
            $url = "http://localhost:3000/login/generic_oauth?state={$state}&code=cc536d98d27750394a87ab9d057016e636a8ac31";
            header("Location: {$url}");
        }

        /**
         * User access token.
         * @return void
         */
        public function token(): void {
            $token = [
                'access_token' => $this->user['access_token'],
                'token_type' => 'Bearer',
                'expiry_in' => '1566172800', // 20.08.2019
                'refresh_token' => $this->user['refresh_token']
            ];

            echo json_encode($token);
        }

        /**
         * User credentials.
         * @return void
         */
        public function user(): void {
            $user = [
                'username' => $this->user['username'],
                'email' => $this->user['email']
            ];

            echo json_encode($user);
        }
    }
Run Code Online (Sandbox Code Playgroud)

oauth/auth.php

<?php
    declare(strict_types=1);

    require __DIR__ . '/../GrafanaOAuth.php';

    /**
     * Fetch the details of Grafana user from your database.
     */
    $user = [
        'username' => 'nbayramberdiyev',
        'email' => 'nbayramberdiyev@outlook.com',
        'dasboard_id' => 'oNNhAtdWz',
        'access_token' => md5(uniqid('nbayramberdiyev', true)),
        'refresh_token' => md5(uniqid('nbayramberdiyev', true))
    ];

    (new GrafanaOAuth($user))->auth($_GET['state']);
Run Code Online (Sandbox Code Playgroud)

oauth/token.php

<?php
    declare(strict_types=1);

    header('Content-Type: application/json');

    require __DIR__ . '/../GrafanaOAuth.php';

    /**
     * Fetch the details of Grafana user from your database.
     */
    $user = [
        'username' => 'nbayramberdiyev',
        'email' => 'nbayramberdiyev@outlook.com',
        'dasboard_id' => 'oNNhAtdWz',
        'access_token' => md5(uniqid('nbayramberdiyev', true)),
        'refresh_token' => md5(uniqid('nbayramberdiyev', true))
    ];

    (new GrafanaOAuth($user))->token();
Run Code Online (Sandbox Code Playgroud)

oauth/user.php

<?php
    declare(strict_types=1);

    header('Content-Type: application/json');

    require __DIR__ . '/../GrafanaOAuth.php';

    /**
     * Fetch the details of Grafana user from your database.
     */
    $user = [
        'username' => 'nbayramberdiyev',
        'email' => 'nbayramberdiyev@outlook.com',
        'dasboard_id' => 'oNNhAtdWz',
        'access_token' => md5(uniqid('nbayramberdiyev', true)),
        'refresh_token' => md5(uniqid('nbayramberdiyev', true))
    ];

    (new GrafanaOAuth($user))->user();
Run Code Online (Sandbox Code Playgroud)

custom.js

$(function() {
    'use strict';

    if (location.pathname === '/login') {
        location.href = $('a.btn-service--oauth').attr('href');
    }
});
Run Code Online (Sandbox Code Playgroud)

步骤2:编辑Grafana配置文件,该文件位于Windows /etc/grafana/grafana.ini上的Ubuntu / Debian,/usr/local/etc/grafana/grafana.iniMAC <GRAFANA_PROJECT_FOLDER>/conf/custom.ini上。

取消注释这些行,并输入你的client_idclient_secretauth_urltoken_urlapi_url

#################################### Generic OAuth ##########################
[auth.generic_oauth]
;enabled = true
;name = OAuth
;allow_sign_up = false
;client_id = some_id
;client_secret = some_secret
;scopes = user:email,read:org
;auth_url =
;token_url =
;api_url =
Run Code Online (Sandbox Code Playgroud)

像这样:

#################################### Generic OAuth ##########################
[auth.generic_oauth]
enabled = true
name = OAuth
allow_sign_up = false
client_id = YOUR_APP_CLIENT_ID
client_secret = YOUR_APP_CLIENT_SECRET
scopes = user:email,read:org
auth_url = http://foo.bar/oauth/auth.php
token_url = http://foo.bar/oauth/token.php
api_url = http://foo.bar/oauth/user.php
Run Code Online (Sandbox Code Playgroud)

步骤3:custom.js/usr/share/grafana/public/build/index.html在底部文件(Ubuntu的/ Debian的)<body>标签。

步骤4:重新启动Grafana服务器。

  • sudo service grafana-server restart (Ubuntu / Debian)
  • brew services restart grafana (苹果电脑)

有关示例和详细说明,请查看我的Github回购