api - 使用 Grafana API 进行自动身份验证

标签 api authentication get grafana grafana-api

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

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

我关注了Automatic login to grafana from web application , Auto login to grafana dashboard , Auto login to grafana from Web application using credentials or token Automatic login by token url ,但没有运气。我找不到合适且干净的解决方案。

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

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

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

最佳答案

经过一些挖掘,我找到了使用 Grafana 的 Generic OAuth Authentication 的解决方法.

第 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);
    }
}

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']);

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();

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();

custom.js :

$(function() {
    'use strict';

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

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

取消注释这些行并输入您的 client_id , client_secret , auth_url , token_url , api_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 =

像这样:

#################################### 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

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

第 4 步: 重启 Grafana 服务器。

  • sudo service grafana-server restart (Ubuntu/Debian)
  • brew services restart grafana (MAC)

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

关于api - 使用 Grafana API 进行自动身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57389522/

相关文章:

authentication - 基于声明的身份验证和用户圈子

c - C 中的登录模拟器,取消引用不完整类型错误

javascript - Spring应用程序在登录后调用最后一个ajax调用

http - AngularJs 和 iTunes

c - 共享内存: SendMessage from VB6 to third-party DLL,从传递的结构中检索数据?

javascript - Facebook Javascript,如何检测用户是否是我的 Facebook 页面的粉丝?在我的网站上?

java - 如何将按钮 Action 监听器绑定(bind)到文本字段

java - GregorianCalendar 类的方法获取?

api - WSO2 openbanking demo api 使用 SMSOTP 返回失败的身份验证

javascript - Google 图表工具日期格式