30日間の無料評価版をお試しいただけます。

Yellowfinをホストアプリケーションと統合する場合、ホストアプリケーションとYellowfin インターフェース間をシームレスに移動させる必要があります。これには、完全なYellowfin UIの統合や、ホストアプリケーション内の個々のコンテンツの組み込みが含まれる可能性があります。いずれの場合も、Yellowfinが毎回ユーザーにログインを求めないようにするには、既存のYellowfin セッションが必要です。これは、YellowfinのSSOサービスで実現できます。

YellowfinのSSOエンドポイントでは、ユーザーがYellowfinのユーザー認証画面をバイパスできるようにする、単一使用のトークンを生成できます。

標準的なユースケースでは、ユーザーは (Yellowfinが統合または組み込まれている) ホストアプリケーションで既に認証されています。ユーザーがYellowfinを必要とするホストアプリケーションの領域に入ろうとすると、ホストアプリケーションはサーバ側でYellowfinを呼び出し、単一使用トークン生成します。トークンを取得したら、YellowfinのログオンURLに追加するか、JS APIコードに埋め込むことができます。ユーザーは、その後、URLまたは組み込みコンテンツを含むページにリダイレクトされます。

Yellowfinの標準ログイン画面をバイパスするには、以下のURLにトークンを追加します。


http://<yellowfin-server-address>/logon.i4?LoginWebserviceId=<tokenId>

SSOログイントークンの生成

ログインSSOトークンは、GET /api/login-tokens エンドポイント Create Login Tokenを使用して取得できます。このリクエストのペイロードは、以下の形式です。

{
  "signOnUser": {
    "userName": "admin@yellowfin.com.au",
    "password": "test",
    "clientOrgRef": "CLIENT1"
  },
  "loginParameters": [
    "YFTOOLBAR=FALSE",
    "ENTRY=VIEWDASHBOARD",
    "DASHBOARDUUID=5678f33c-c666-4972-8976-9c489accf73b"
  ],
  "noPassword": false,
  "customParameters": "Some custom integration value"
}

signOnUser属性は必須ですが、その他の属性はオプションです。signOnUser属性のコンテンツは、ログインするユーザーの資格情報になります。clientOrgRefは、テナントにログインする場合にのみ必要です。

以下の例では、ユーザー名とパスワードの入力を求めることなく、Yellowfinにユーザーをログインさせるために使用できるSSOトークンを返します。こちらの例では、REST アクセストークンを使用する標準的なパラダイムを使用しています。

Java
package rest.code.examples;
import java.io.IOException;
import java.util.Random;
import org.apache.hc.client5.http.fluent.Content;
import org.apache.hc.client5.http.fluent.Request;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
/**
 * Create a user using the Yellowfin REST API
 */
public class CreateSSOTokenWithAccessToken {
    public static void main(String[] args) throws Exception {
 
        String host = "http://localhost:8080/Yellowfin";
        String restUsername = "admin@yellowfin.com.au";
        String restPassword = "test";
 
        String userToLogin = "user1@yellowfin.com.au";
        String userToLoginPassword = "test";
 
        String createUserPayload = "{\n"
                + "  \"signOnUser\": {\n"
                + "    \"userName\": \""+ userToLogin + "\",\n"
                + "    \"password\": \""+ userToLoginPassword + "\"\n"
                + "  }\n"
                + "}";
 
        String token = generateToken(host, restUsername, restPassword);
 
        System.out.println("Payload: " + createUserPayload);
 
        Content c = Request.post(host + "/api/login-tokens")
                .addHeader("Authorization", "YELLOWFIN ts=" + System.currentTimeMillis() + " , nonce=" + new Random().nextLong() + ", token=" + token)
                .addHeader("Accept", "application/vnd.yellowfin.api-v1+json")
                .addHeader("Content-Type", "application/json")
                .bodyString(createUserPayload, null)
                .execute().returnContent();
 
        JsonObject jsonObject = new JsonParser().parse(c.asString()).getAsJsonObject();
        JsonElement securityToken = jsonObject.get("securityToken");
 
        System.out.println("SSO Token: " + securityToken);
 
    }
 
    public static String generateToken(String host, String username, String password) throws IOException {
 
        Content c = Request.post(host + "/api/refresh-tokens")
                .addHeader("Authorization", "YELLOWFIN ts=" + System.currentTimeMillis() + " , nonce=" + new Random().nextLong())
                .addHeader("Accept", "application/vnd.yellowfin.api-v1+json")
                .addHeader("Content-Type", "application/json")
                .bodyString("{ \"userName\": \""+ username + "\",\"password\": \""+ password + "\"}", null)
                .execute().returnContent();
 
        JsonObject jsonObject = new JsonParser().parse(c.asString()).getAsJsonObject();
        JsonElement accessToken = jsonObject.getAsJsonObject("_embedded").getAsJsonObject("accessToken").get("securityToken");
 
        if (accessToken!=null) {
            System.out.println("Access Token: " + accessToken);
        } else {
            System.out.println("Token not retrieved successfully");
            System.exit(-1);
        }
        return accessToken.getAsString();
 
    }
 
}
C#
using System.Net.Http.Headers;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
 
namespace YellowfinAPIExamples
{
    public class CreateSSOTokenWithAccessToken
    {
        static async Task Main(string[] args)
        {
            string host = "http://localhost:8080/Yellowfin";
            string restUsername = "admin@yellowfin.com.au";
            string restPassword = "test";
            string userToLogin = "user1@yellowfin.com.au";
            string userToLoginPassword = "test";
 
            string createUserPayload = "{\n"
                + "  \"signOnUser\": {\n"
                + "    \"userName\": \"" + userToLogin + "\",\n"
                + "    \"password\": \"" + userToLoginPassword + "\"\n"
                + "  }\n"
                + "}";
 
            string token = await GenerateToken(host, restUsername, restPassword);
 
            Console.WriteLine("Payload: " + createUserPayload);
 
            using (var httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("YELLOWFIN", "ts=" + DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() + " , nonce=" + new Random().NextInt64() + ", token=" + token);
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.yellowfin.api-v1+json"));
 
                var content = new StringContent(createUserPayload, System.Text.Encoding.UTF8, "application/json");
 
                HttpResponseMessage response = await httpClient.PostAsync(host + "/api/login-tokens", content);
                if (response.IsSuccessStatusCode)
                {
                    string responseBody = await response.Content.ReadAsStringAsync();
                    JObject jsonObject = JsonConvert.DeserializeObject<JObject>(responseBody);
                    string securityToken = jsonObject["securityToken"].ToString();
                    Console.WriteLine("SSO Token: " + securityToken);
                }
                else
                {
                    Console.WriteLine("Failed to create SSO token. Status code: " + response.StatusCode);
                }
            }
        }
 
        static async Task<string> GenerateToken(string host, string restUsername, string restPassword)
        {
            using (var client = new HttpClient())
            {
                long nonce = new Random().NextInt64();
 
                var request = new HttpRequestMessage(HttpMethod.Post, host + "/api/refresh-tokens");
                request.Headers.Add("Authorization", "YELLOWFIN ts=" + DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() + ", nonce=" + nonce);
                request.Headers.Add("Accept", "application/vnd.yellowfin.api-v1+json");
                request.Content = new StringContent(
                    JsonConvert.SerializeObject(new { userName = restUsername, password = restPassword }),
                    System.Text.Encoding.UTF8,
                    "application/json"
                );
 
                HttpResponseMessage response = await client.SendAsync(request);
                string responseContent = await response.Content.ReadAsStringAsync();
 
                JObject jsonObject = JsonConvert.DeserializeObject<JObject>(responseContent);
                string accessToken = jsonObject["_embedded"]["accessToken"]["securityToken"].ToString();
 
                if (!string.IsNullOrEmpty(accessToken))
                {
                    Console.WriteLine("Access Token: " + accessToken);
                    return accessToken;
                }
                else
                {
                    Console.WriteLine("Token not retrieved successfully");
                    Environment.Exit(-1);
                }
            }
 
            return null; // Should not reach here due to Environment.Exit
        }
    }
}
Go
package main
 
import (
    "bytes"
    "encoding/json"
    "fmt"
    "io/ioutil"
    "math/rand"
    "net/http"
    "time"
)
 
func main() {
    host := "http://localhost:8080/Yellowfin"
    restUsername := "admin@yellowfin.com.au"
    restPassword := "test"
 
    userToLogin := "user1@yellowfin.com.au"
    userToLoginPassword := "test"
 
    createUserPayload := fmt.Sprintf(`{
        "signOnUser": {
            "userName": "%s",
            "password": "%s"
        }
    }`, userToLogin, userToLoginPassword)
 
    token, err := generateToken(host, restUsername, restPassword)
    if err != nil {
        fmt.Println("Error generating token:", err)
        return
    }
 
    fmt.Println("Payload:", createUserPayload)
 
    client := &http.Client{}
    req, err := http.NewRequest("POST", host+"/api/login-tokens", bytes.NewBuffer([]byte(createUserPayload)))
    if err != nil {
        fmt.Println("Error creating request:", err)
        return
    }
 
    nonce := rand.Int63()
 
    req.Header.Set("Authorization", fmt.Sprintf("YELLOWFIN ts=%d, nonce=%d, token=%s", time.Now().UnixMilli(), nonce, token))
    req.Header.Set("Accept", "application/vnd.yellowfin.api-v1+json")
    req.Header.Set("Content-Type", "application/json")
 
    resp, err := client.Do(req)
    if err != nil {
        fmt.Println("Error sending request:", err)
        return
    }
    defer resp.Body.Close()
 
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Println("Error reading response body:", err)
        return
    }
 
    var jsonObject map[string]interface{}
    err = json.Unmarshal(body, &jsonObject)
    if err != nil {
        fmt.Println("Error parsing JSON response:", err)
        return
    }
 
    securityToken, ok := jsonObject["securityToken"].(string)
    if !ok {
        fmt.Println("Token not retrieved successfully")
        return
    }
 
    fmt.Println("SSO Token:", securityToken)
}
 
func generateToken(host, restUsername, restPassword string) (string, error) {
    nonce := rand.Int63()
 
    requestBody, err := json.Marshal(map[string]string{
        "userName": restUsername,
        "password": restPassword,
    })
    if err != nil {
        fmt.Println("Error marshaling request body:", err)
        return "", err
    }
 
    client := &http.Client{}
 
    request, err := http.NewRequest("POST", host+"/api/refresh-tokens", bytes.NewBuffer(requestBody))
    if err != nil {
        fmt.Println("Error creating request:", err)
        return "", err
    }
 
    request.Header.Set("Authorization", fmt.Sprintf("YELLOWFIN ts=%d, nonce=%d", time.Now().UnixMilli(), nonce))
    request.Header.Set("Accept", "application/vnd.yellowfin.api-v1+json")
    request.Header.Set("Content-Type", "application/json")
 
    response, err := client.Do(request)
    if err != nil {
        fmt.Println("Error sending request:", err)
        return "", err
    }
    defer response.Body.Close()
 
    responseBody, err := ioutil.ReadAll(response.Body)
    if err != nil {
        fmt.Println("Error reading response body:", err)
        return "", err
    }
 
    var jsonResponse map[string]interface{}
    err = json.Unmarshal(responseBody, &jsonResponse)
    if err != nil {
        fmt.Println("Error parsing JSON response:", err)
        return "", err
    }
 
    accessToken, ok := jsonResponse["_embedded"].(map[string]interface{})["accessToken"].(map[string]interface{})["securityToken"].(string)
    if !ok {
        fmt.Println("Token not retrieved successfully")
        return "", fmt.Errorf("Token not retrieved successfully")
    }
 
    return accessToken, nil
}
JavaScript
const fetch = require("node-fetch");
 
async function main() {
    const host = "http://localhost:8080/Yellowfin";
    const restUsername = "admin@yellowfin.com.au";
    const restPassword = "test";
 
    const userToLogin = "user1@yellowfin.com.au";
    const userToLoginPassword = "test";
 
    const createUserPayload = JSON.stringify({
        signOnUser: {
            userName: userToLogin,
            password: userToLoginPassword
        }
    });
 
    const token = await generateToken(host, restUsername, restPassword);
 
    if (token === null) {
        console.error("Failed to retrieve access token");
        return;
    }
 
    console.log("Payload:", createUserPayload);
 
    const nonce = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
 
    const headers = {
        'Authorization': `YELLOWFIN ts=${Date.now()}, nonce=${nonce}, token=${token}`,
        'Accept': 'application/vnd.yellowfin.api-v1+json',
        'Content-Type': 'application/json'
    };
 
    try {
        const response = await fetch(`${host}/api/login-tokens`, {
            method: 'POST',
            headers: headers,
            body: createUserPayload
        });
 
        if (!response.ok) {
            throw new Error(`HTTP error! Status: ${response.status}`);
        }
 
        const jsonResponse = await response.json();
        const securityToken = jsonResponse.securityToken;
 
        if (securityToken) {
            console.log("SSO Token:", securityToken);
        } else {
            console.error("Failed to retrieve SSO token");
        }
    } catch (error) {
        console.error("Error:", error.message);
    }
}
 
async function generateToken(host, restUsername, restPassword) {
    const nonce = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
 
    const headers = {
        'Authorization': `YELLOWFIN ts=${Date.now()}, nonce=${nonce}`,
        'Accept': 'application/vnd.yellowfin.api-v1+json',
        'Content-Type': 'application/json'
    };
 
    const body = JSON.stringify({
        userName: restUsername,
        password: restPassword
    });
 
    try {
        const response = await fetch(`${host}/api/refresh-tokens`, {
            method: 'POST',
            headers: headers,
            body: body
        });
 
        if (!response.ok) {
            throw new Error(`HTTP error! Status: ${response.status}`);
        }
 
        const jsonResponse = await response.json();
        const accessToken = jsonResponse._embedded.accessToken.securityToken;
 
        if (accessToken) {
            console.log("Access Token:", accessToken);
        } else {
            console.error("Token not retrieved");
            return null;
        }
 
        return accessToken;
    } catch (error) {
        console.error("Error:", error.message);
        return null;
    }
}
 
main();
PHP
<?php
function main() {
    $host = "http://localhost:8080/Yellowfin";
    $restUsername = "admin@yellowfin.com.au";
    $restPassword = "test";
 
    $userToLogin = "user1@yellowfin.com.au";
    $userToLoginPassword = "test";
 
    $createUserPayload = json_encode(array(
        "signOnUser" => array(
            "userName" => $userToLogin,
            "password" => $userToLoginPassword
        )
    ));
 
    try {
        $token = generateToken($host, $restUsername, $restPassword);
    } catch (Exception $e) {
        echo "Error generating token: " . $e->getMessage();
        return;
    }
 
    echo "Payload: " . $createUserPayload . "\n";
 
    $nonce = mt_rand();
    $headers = array(
        'Authorization: YELLOWFIN ts=' . intval(microtime(true) * 1000) . ', nonce=' . $nonce . ', token=' . $token,
        'Accept: application/vnd.yellowfin.api-v1+json',
        'Content-Type: application/json'
    );
 
    try {
        $response = httpRequest('POST', "$host/api/login-tokens", $headers, $createUserPayload);
        $jsonResponse = json_decode($response, true);
        if (isset($jsonResponse['securityToken'])) {
            echo "SSO Token: " . $jsonResponse['securityToken'] . "\n";
        } else {
            echo "Failed to retrieve SSO token\n";
        }
    } catch (Exception $e) {
        echo "Error sending request: " . $e->getMessage() . "\n";
    }
}
 
function generateToken($host, $restUsername, $restPassword) {
    $nonce = mt_rand();
 
    $requestBody = json_encode(array(
        "userName" => $restUsername,
        "password" => $restPassword
    ));
 
    $headers = array(
        'Authorization: YELLOWFIN ts=' . intval(microtime(true) * 1000) . ', nonce=' . $nonce,
        'Accept: application/vnd.yellowfin.api-v1+json',
        'Content-Type: application/json'
    );
 
    $response = httpRequest('POST', "$host/api/refresh-tokens", $headers, $requestBody);
 
    $jsonResponse = json_decode($response, true);
 
    if (isset($jsonResponse["_embedded"]["accessToken"]["securityToken"])) {
        $accessToken = $jsonResponse["_embedded"]["accessToken"]["securityToken"];
        echo "Access Token: " . $accessToken . "\n";
        return $accessToken;
    } else {
        throw new Exception("Token not retrieved successfully");
    }
}
 
function httpRequest($method, $url, $headers, $data = null) {
    $ch = curl_init();
 
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
 
    if ($data !== null) {
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
 
    $response = curl_exec($ch);
 
    if (curl_errno($ch)) {
        throw new Exception('Error: ' . curl_error($ch));
    }
 
    curl_close($ch);
 
    return $response;
}
 
main();
?>
Python
import json
import random
import time
 
import requests
 
def main():
    host = "http://localhost:8080/Yellowfin"
    rest_username = "admin@yellowfin.com.au"
    rest_password = "test"
 
    user_to_login = "user1@yellowfin.com.au"
    user_to_login_password = "test"
 
    create_user_payload = json.dumps({
        "signOnUser": {
            "userName": user_to_login,
            "password": user_to_login_password
        }
    })
 
    try:
        token = generate_token(host, rest_username, rest_password)
    except Exception as e:
        print(f"Error generating token: {e}")
        return
 
    print("Payload:", create_user_payload)
 
    nonce = random.randint(0, 2 ** 63 - 1)
 
    headers = {
        'Authorization': f'YELLOWFIN ts={int(time.time() * 1000)}, nonce={nonce}, token={token}',
        'Accept': 'application/vnd.yellowfin.api-v1+json',
        'Content-Type': 'application/json'
    }
 
    try:
        response = requests.post(host + "/api/login-tokens", headers=headers, data=create_user_payload)
        response.raise_for_status()
        json_response = response.json()
        security_token = json_response.get("securityToken")
        print("SSO Token:", security_token)
    except requests.RequestException as e:
        print(f"Error sending request: {e}")
 
 
def generate_token(host, rest_username, rest_password):
    nonce = random.randint(0, 2 ** 63 - 1)
 
    # Create request body
    request_body = json.dumps({
        "userName": rest_username,
        "password": rest_password
    })
 
    # Create request headers
    headers = {
        'Authorization': f'YELLOWFIN ts={int(time.time() * 1000)}, nonce={nonce}',
        'Accept': 'application/vnd.yellowfin.api-v1+json',
        'Content-Type': 'application/json'
    }
 
    # Send HTTP request
    response = requests.post(host + "/api/refresh-tokens", headers=headers, data=request_body)
 
    # Check response status
    if response.status_code == 200:
        # Parse JSON response
        json_response = response.json()
        access_token = json_response["_embedded"]["accessToken"]["securityToken"]
        print("Access Token:", access_token)
        return access_token
    else:
        raise Exception("Token not retrieved successfully")
 
if __name__ == "__main__":
    main()

テナントにアクセスするためのSSOログイントークンの生成

clientOrgRefを指定すると、テナントでユーザーセッションを作成するためのトークンが生成されます。clientOrgRefが指定されておらず、ユーザーが複数のテナントのメンバーである場合、そのユーザーには、ログインプロセス中にクライアント組織の選択画面が表示されます。ユーザーが1つのテナントのメンバーである場合、ユーザーはSSOプロセス中にクライアント組織を選択するように求められることはありません。

ユーザーがそのテナントにログインできるように、clientOrgRefにテナントのクライアント組織参照コードを設定する必要があります。

{
  "signOnUser": {
    "userName": "admin@yellowfin.com.au",
    "password": "test",
    "clientOrgRef": "CLIENT1"
  }
}

以下の例では、ユーザー名とパスワードの入力を求めることなく、Yellowfin テナントにユーザーをログインさせるために使用できるSSOトークンを返します。これらの例では、REST アクセストークンを使用する標準的なパラダイムを使用します。

Java
package rest.code.examples;
import java.io.IOException;
import java.util.Random;
import org.apache.hc.client5.http.fluent.Content;
import org.apache.hc.client5.http.fluent.Request;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
/**
 * Create a SSO login token for use with a tenant using the Yellowfin REST API
 */
public class CreateSSOTokenWithAccessTokenIntoTenant {
    public static void main(String[] args) throws Exception {
 
        String host = "http://localhost:8080/Yellowfin";
        String restUsername = "admin@yellowfin.com.au";
        String restPassword = "test";
 
        String userToLogin = "user1@yellowfin.com.au";
        String userToLoginPassword = "test";
        String clientOrgReference = "CLIENT1";
 
        String createUserPayload = "{\n"
                + "  \"signOnUser\": {\n"
                + "    \"userName\": \""+ userToLogin + "\",\n"
                + "    \"password\": \""+ userToLoginPassword + "\",\n"
                + "    \"clientOrgRef\": \""+ clientOrgReference + "\"\n"
                + "  }\n"
                + "}";
 
        String token = generateToken(host, restUsername, restPassword);
 
        System.out.println("Payload: " + createUserPayload);
 
        Content c = Request.post(host + "/api/login-tokens")
                .addHeader("Authorization", "YELLOWFIN ts=" + System.currentTimeMillis() + " , nonce=" + new Random().nextLong() + ", token=" + token)
                .addHeader("Accept", "application/vnd.yellowfin.api-v1+json")
                .addHeader("Content-Type", "application/json")
                .bodyString(createUserPayload, null)
                .execute().returnContent();
 
        JsonObject jsonObject = new JsonParser().parse(c.asString()).getAsJsonObject();
        JsonElement securityToken = jsonObject.get("securityToken");
 
        System.out.println("SSO Token: " + securityToken);
 
    }
 
    public static String generateToken(String host, String username, String password) throws IOException {
 
        Content c = Request.post(host + "/api/refresh-tokens")
                .addHeader("Authorization", "YELLOWFIN ts=" + System.currentTimeMillis() + " , nonce=" + new Random().nextLong())
                .addHeader("Accept", "application/vnd.yellowfin.api-v1+json")
                .addHeader("Content-Type", "application/json")
                .bodyString("{ \"userName\": \""+ username + "\",\"password\": \""+ password + "\"}", null)
                .execute().returnContent();
 
        JsonObject jsonObject = new JsonParser().parse(c.asString()).getAsJsonObject();
        JsonElement accessToken = jsonObject.getAsJsonObject("_embedded").getAsJsonObject("accessToken").get("securityToken");
 
        if (accessToken!=null) {
            System.out.println("Access Token: " + accessToken);
        } else {
            System.out.println("Token not retrieved successfully");
            System.exit(-1);
        }
        return accessToken.getAsString();
 
    }
 
}
C#
using System.Net.Http.Headers;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
 
namespace YellowfinAPIExamples
{
    public class CreateSSOTokenWithAccessTokenIntoTenant
    {
        static async Task Main(string[] args)
        {
            string host = "http://localhost:8080/Yellowfin";
            string restUsername = "admin@yellowfin.com.au";
            string restPassword = "test";
            string userToLogin = "user1@yellowfin.com.au";
            string userToLoginPassword = "test";
            string clientOrgReference = "CLIENT1";
 
            string createUserPayload = "{\n"
                                       + "  \"signOnUser\": {\n"
                                       + "    \"userName\": \"" + userToLogin + "\",\n"
                                       + "    \"password\": \"" + userToLoginPassword + "\",\n"
                                       + "    \"clientOrgRef\": \"" + clientOrgReference + "\"\n"
                                       + "  }\n"
                                       + "}";
 
            string token = await GenerateToken(host, restUsername, restPassword);
 
            Console.WriteLine("Payload: " + createUserPayload);
 
            using (var httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("YELLOWFIN", "ts=" + DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() + " , nonce=" + new Random().NextInt64() + ", token=" + token);
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.yellowfin.api-v1+json"));
 
                var content = new StringContent(createUserPayload, System.Text.Encoding.UTF8, "application/json");
 
                HttpResponseMessage response = await httpClient.PostAsync(host + "/api/login-tokens", content);
                if (response.IsSuccessStatusCode)
                {
                    string responseBody = await response.Content.ReadAsStringAsync();
                    JObject jsonObject = JsonConvert.DeserializeObject<JObject>(responseBody);
                    string securityToken = jsonObject["securityToken"].ToString();
                    Console.WriteLine("SSO Token: " + securityToken);
                }
                else
                {
                    Console.WriteLine("Failed to create SSO token. Status code: " + response.StatusCode);
                }
            }
        }
 
        static async Task<string> GenerateToken(string host, string restUsername, string restPassword)
        {
            using (var client = new HttpClient())
            {
                // Generate nonce
                long nonce = new Random().NextInt64();
 
                // Create HTTP request
                var request = new HttpRequestMessage(HttpMethod.Post, host + "/api/refresh-tokens");
                request.Headers.Add("Authorization", "YELLOWFIN ts=" + DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() + ", nonce=" + nonce);
                request.Headers.Add("Accept", "application/vnd.yellowfin.api-v1+json");
                request.Content = new StringContent(
                    JsonConvert.SerializeObject(new { userName = restUsername, password = restPassword }),
                    System.Text.Encoding.UTF8,
                    "application/json"
                );
 
                // Send request and get response
                HttpResponseMessage response = await client.SendAsync(request);
                string responseContent = await response.Content.ReadAsStringAsync();
 
                // Parse JSON response
                JObject jsonObject = JsonConvert.DeserializeObject<JObject>(responseContent);
                string accessToken = jsonObject["_embedded"]["accessToken"]["securityToken"].ToString();
 
                if (!string.IsNullOrEmpty(accessToken))
                {
                    Console.WriteLine("Access Token: " + accessToken);
                }
                else
                {
                    Console.WriteLine("Token not retrieved");
                    Environment.Exit(-1);
                }
 
                return accessToken;
            }
        }
    }
}
Go
package main
 
import (
    "bytes"
    "encoding/json"
    "fmt"
    "io/ioutil"
    "math/rand"
    "net/http"
    "time"
)
 
func main() {
    host := "http://localhost:8080/Yellowfin"
    restUsername := "admin@yellowfin.com.au"
    restPassword := "test"
 
    userToLogin := "user1@yellowfin.com.au"
    userToLoginPassword := "test"
    clientOrgReference := "CLIENT1"
 
    createUserPayload := `{
  "signOnUser": {
    "userName": "` + userToLogin + `",
    "password": "` + userToLoginPassword + `",
    "clientOrgRef": "` + clientOrgReference + `"
  }
}`
 
    token, err := generateToken(host, restUsername, restPassword)
    if err != nil {
        fmt.Println("Error generating token:", err)
        return
    }
 
    fmt.Println("Payload:", createUserPayload)
 
    nonce := rand.Int63()
 
    req, err := http.NewRequest("POST", host+"/api/login-tokens", bytes.NewBuffer([]byte(createUserPayload)))
    if err != nil {
        fmt.Println("Error creating request:", err)
        return
    }
 
    req.Header.Set("Authorization", fmt.Sprintf("YELLOWFIN ts=%d, nonce=%d, token=%s", time.Now().UnixMilli(), nonce, token))
    req.Header.Set("Accept", "application/vnd.yellowfin.api-v1+json")
    req.Header.Set("Content-Type", "application/json")
 
    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        fmt.Println("Error sending request:", err)
        return
    }
    defer resp.Body.Close()
 
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Println("Error reading response body:", err)
        return
    }
 
    var jsonResponse map[string]interface{}
    err = json.Unmarshal(body, &jsonResponse)
    if err != nil {
        fmt.Println("Error parsing JSON response:", err)
        return
    }
 
    securityToken, ok := jsonResponse["securityToken"].(string)
    if !ok {
        fmt.Println("SSO Token not retrieved")
        return
    }
 
    fmt.Println("SSO Token:", securityToken)
}
 
func generateToken(host, restUsername, restPassword string) (string, error) {
    nonce := rand.Int63()
 
    requestBody, err := json.Marshal(map[string]string{
        "userName": restUsername,
        "password": restPassword,
    })
    if err != nil {
        fmt.Println("Error marshaling request body:", err)
        return "", err
    }
 
    client := &http.Client{}
 
    request, err := http.NewRequest("POST", host+"/api/refresh-tokens", bytes.NewBuffer(requestBody))
    if err != nil {
        fmt.Println("Error creating request:", err)
        return "", err
    }
 
    request.Header.Set("Authorization", fmt.Sprintf("YELLOWFIN ts=%d, nonce=%d", time.Now().UnixMilli(), nonce))
    request.Header.Set("Accept", "application/vnd.yellowfin.api-v1+json")
    request.Header.Set("Content-Type", "application/json")
 
    response, err := client.Do(request)
    if err != nil {
        fmt.Println("Error sending request:", err)
        return "", err
    }
    defer response.Body.Close()
 
    responseBody, err := ioutil.ReadAll(response.Body)
    if err != nil {
        fmt.Println("Error reading response body:", err)
        return "", err
    }
 
    var jsonResponse map[string]interface{}
    err = json.Unmarshal(responseBody, &jsonResponse)
    if err != nil {
        fmt.Println("Error parsing JSON response:", err)
        return "", err
    }
 
    accessToken, ok := jsonResponse["_embedded"].(map[string]interface{})["accessToken"].(map[string]interface{})["securityToken"].(string)
    if !ok {
        fmt.Println("Token not retrieved")
        return "", fmt.Errorf("Token not retrieved successfully")
    }
 
    return accessToken, nil
}
JavaScript
const fetch = require("node-fetch");
 
async function main() {
    const host = "http://localhost:8080/Yellowfin";
    const restUsername = "admin@yellowfin.com.au";
    const restPassword = "test";
 
    const userToLogin = "user1@yellowfin.com.au";
    const userToLoginPassword = "test";
 
    const createUserPayload = JSON.stringify({
        signOnUser: {
            userName: userToLogin,
            password: userToLoginPassword,
            clientOrgRef: "CLIENT1"
        }
    });
 
    const token = await generateToken(host, restUsername, restPassword);
 
    if (token === null) {
        console.error("Failed to retrieve access token");
        return;
    }
 
    console.log("Payload:", createUserPayload);
 
    const nonce = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
 
    const headers = {
        'Authorization': `YELLOWFIN ts=${Date.now()}, nonce=${nonce}, token=${token}`,
        'Accept': 'application/vnd.yellowfin.api-v1+json',
        'Content-Type': 'application/json'
    };
 
    try {
        const response = await fetch(`${host}/api/login-tokens`, {
            method: 'POST',
            headers: headers,
            body: createUserPayload
        });
 
        if (!response.ok) {
            throw new Error(`HTTP error! Status: ${response.status}`);
        }
 
        const responseBody = await response.text();
        const jsonObject = JSON.parse(responseBody);
        const securityToken = jsonObject.securityToken;
 
        console.log("SSO Token:", securityToken);
    } catch (error) {
        console.error("Error:", error.message);
    }
}
 
async function generateToken(host, restUsername, restPassword) {
    const nonce = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
 
    const headers = {
        'Authorization': `YELLOWFIN ts=${Date.now()}, nonce=${nonce}`,
        'Accept': 'application/vnd.yellowfin.api-v1+json',
        'Content-Type': 'application/json'
    };
 
    const body = JSON.stringify({
        userName: restUsername,
        password: restPassword
    });
 
    try {
        const response = await fetch(`${host}/api/refresh-tokens`, {
            method: 'POST',
            headers: headers,
            body: body
        });
 
        if (!response.ok) {
            throw new Error(`HTTP error! Status: ${response.status}`);
        }
 
        const jsonResponse = await response.json();
        const accessToken = jsonResponse._embedded.accessToken.securityToken;
 
        if (accessToken) {
            console.log(`Access Token: ${accessToken}`);
        } else {
            console.log("Token not retrieved");
        }
 
        return accessToken;
    } catch (error) {
        console.error("Error:", error.message);
    }
 
    return null;
}
 
main();
PHP
<?php
function main() {
    $host = "http://localhost:8080/Yellowfin";
    $restUsername = "admin@yellowfin.com.au";
    $restPassword = "test";
 
    // Create user payload
    $createUserPayload = '{
        "signOnUser": {
            "userName": "user1@yellowfin.com.au",
            "password": "test",
            "clientOrgRef": "CLIENT1"
        }
    }';
 
    try {
        $token = generateToken($host, $restUsername, $restPassword);
    } catch (Exception $e) {
        echo "Error generating token: " . $e->getMessage();
        return;
    }
 
    echo "Payload: " . $createUserPayload . "\n";
 
    $nonce = mt_rand();
    $headers = array(
        'Authorization: YELLOWFIN ts=' . intval(microtime(true) * 1000) . ', nonce=' . $nonce . ', token=' . $token,
        'Accept: application/vnd.yellowfin.api-v1+json',
        'Content-Type: application/json'
    );
 
    try {
        $response = httpRequest('POST', "$host/api/login-tokens", $headers, $createUserPayload);
        $jsonResponse = json_decode($response, true);
        if (isset($jsonResponse['securityToken'])) {
            echo "SSO Token: " . $jsonResponse['securityToken'] . "\n";
        } else {
            echo "Failed to retrieve SSO token\n";
        }
    } catch (Exception $e) {
        echo "Error sending request: " . $e->getMessage();
    }
}
 
function generateToken($host, $restUsername, $restPassword) {
    // Generate nonce
    $nonce = mt_rand();
 
    // Create request body
    $requestBody = '{
        "userName": "' . $restUsername . '",
        "password": "' . $restPassword . '"
    }';
 
    // Create request headers
    $headers = array(
        'Authorization: YELLOWFIN ts=' . intval(microtime(true) * 1000) . ', nonce=' . $nonce,
        'Accept: application/vnd.yellowfin.api-v1+json',
        'Content-Type: application/json'
    );
 
    // Send HTTP request
    $response = httpRequest('POST', "$host/api/refresh-tokens", $headers, $requestBody);
 
    // Parse JSON response
    $jsonResponse = json_decode($response, true);
 
    // Get access token from response
    if (isset($jsonResponse["_embedded"]["accessToken"]["securityToken"])) {
        $accessToken = $jsonResponse["_embedded"]["accessToken"]["securityToken"];
        echo "Access Token: " . $accessToken;
 
        return $accessToken;
    } else {
        throw new Exception("Token not retrieved successfully");
    }
}
 
function httpRequest($method, $url, $headers, $data = null) {
    $ch = curl_init();
 
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
 
    if ($data !== null) {
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
 
    $response = curl_exec($ch);
 
    if (curl_errno($ch)) {
        throw new Exception('Error: ' . curl_error($ch));
    }
 
    curl_close($ch);
 
    return $response;
}
 
main()
?>
Python

 

特定のページに入るためのSSOログイントークンの生成

SSO ペイロードのloginParametersでは、トークンを生成する新しいセッションのオプションを設定できます。これは、UI要素の表示/非表示、セキュリティパラメーターの設定、ユーザーログイン時にどのページに移動するかの設定などに使用できます。こちらのペイロードの例では、ユーザーのエントリーが、指定されたダッシュボードUUIDを持つダッシュボードになることを示しています。

{
  "signOnUser": {
    "userName": "user1@yellowfin.com.au",
    "password": "test"
  },
  "loginParameters": [
    "ENTRY=VIEWDASHBOARD",
    "DASHBOARDUUID=5678f33c-c666-4972-8976-9c489accf73b"
  ]
}

以下のコード例では、指定したダッシュボードエントリーポイントでSSOトークンを生成します。

Java
package rest.code.examples;
import java.io.IOException;
import java.util.Random;
import org.apache.hc.client5.http.fluent.Content;
import org.apache.hc.client5.http.fluent.Request;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
/**
 * Create a targeted SSO login token using the Yellowfin REST API
 */
public class CreateSSOTokenWithAccessTokenToPage {
    public static void main(String[] args) throws Exception {
 
        String host = "http://localhost:8080/Yellowfin";
        String restUsername = "admin@yellowfin.com.au";
        String restPassword = "test";
 
        String userToLogin = "user1@yellowfin.com.au";
        String userToLoginPassword = "test";
        String entryDashboardUUID = "321e5a85-a349-4cfb-b8f4-c9141059a66a";
 
        String createUserPayload = "{\n"
                + "  \"signOnUser\": {\n"
                + "    \"userName\": \""+ userToLogin + "\",\n"
                + "    \"password\": \""+ userToLoginPassword + "\"\n"
                + "  },\n"
                + "   \"loginParameters\": ["
                + "   \"ENTRY=VIEWDASHBOARD\","
                + "\"DASHBOARDUUID=" + entryDashboardUUID + "\""
                + " ] "
                + "}";
 
        String token = generateToken(host, restUsername, restPassword);
 
        System.out.println("Payload: " + createUserPayload);
 
        Content c = Request.post(host + "/api/login-tokens")
                .addHeader("Authorization", "YELLOWFIN ts=" + System.currentTimeMillis() + " , nonce=" + new Random().nextLong() + ", token=" + token)
                .addHeader("Accept", "application/vnd.yellowfin.api-v1+json")
                .addHeader("Content-Type", "application/json")
                .bodyString(createUserPayload, null)
                .execute().returnContent();
 
        JsonObject jsonObject = new JsonParser().parse(c.asString()).getAsJsonObject();
        JsonElement securityToken = jsonObject.get("securityToken");
 
        System.out.println("SSO Token: " + securityToken);
 
    }
 
    public static String generateToken(String host, String username, String password) throws IOException {
 
        Content c = Request.post(host + "/api/refresh-tokens")
                .addHeader("Authorization", "YELLOWFIN ts=" + System.currentTimeMillis() + " , nonce=" + new Random().nextLong())
                .addHeader("Accept", "application/vnd.yellowfin.api-v1+json")
                .addHeader("Content-Type", "application/json")
                .bodyString("{ \"userName\": \""+ username + "\",\"password\": \""+ password + "\"}", null)
                .execute().returnContent();
 
        JsonObject jsonObject = new JsonParser().parse(c.asString()).getAsJsonObject();
        JsonElement accessToken = jsonObject.getAsJsonObject("_embedded").getAsJsonObject("accessToken").get("securityToken");
 
        if (accessToken!=null) {
            System.out.println("Access Token: " + accessToken);
        } else {
            System.out.println("Token not retrieved successfully");
            System.exit(-1);
        }
        return accessToken.getAsString();
 
    }
 
}
C#
using System.Net.Http.Headers;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
 
namespace YellowfinAPIExamples
{
    public class CreateSSOTokenWithAccessTokenToPage
    {
        static async Task Main(string[] args)
        {
            string host = "http://localhost:8080/Yellowfin";
            string restUsername = "admin@yellowfin.com.au";
            string restPassword = "test";
            string userToLogin = "user1@yellowfin.com.au";
            string userToLoginPassword = "test";
            string entryDashboardUUID = "321e5a85-a349-4cfb-b8f4-c9141059a66a";
 
            string createUserPayload = "{\n"
                + "  \"signOnUser\": {\n"
                + "    \"userName\": \"" + userToLogin + "\",\n"
                + "    \"password\": \"" + userToLoginPassword + "\"\n"
                + "  },\n"
                + "   \"loginParameters\": ["
                + "   \"ENTRY=VIEWDASHBOARD\","
                + "\"DASHBOARDUUID=" + entryDashboardUUID + "\""
                + " ] "
                + "}";
 
            string token = await GenerateToken(host, restUsername, restPassword);
 
            Console.WriteLine("Payload: " + createUserPayload);
 
            using (var httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("YELLOWFIN", "ts=" + DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() + " , nonce=" + new Random().NextInt64() + ", token=" + token);
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.yellowfin.api-v1+json"));
 
                var content = new StringContent(createUserPayload, System.Text.Encoding.UTF8, "application/json");
 
                HttpResponseMessage response = await httpClient.PostAsync(host + "/api/login-tokens", content);
                if (response.IsSuccessStatusCode)
                {
                    string responseBody = await response.Content.ReadAsStringAsync();
                    JObject jsonObject = JsonConvert.DeserializeObject<JObject>(responseBody);
                    string securityToken = jsonObject["securityToken"].ToString();
                    Console.WriteLine("SSO Token: " + securityToken);
                }
                else
                {
                    Console.WriteLine("Failed to create SSO token. Status code: " + response.StatusCode);
                }
            }
        }
 
        static async Task<string> GenerateToken(string host, string restUsername, string restPassword)
        {
            using (var client = new HttpClient())
            {
                long nonce = new Random().NextInt64();
 
                var request = new HttpRequestMessage(HttpMethod.Post, host + "/api/refresh-tokens");
                request.Headers.Add("Authorization", "YELLOWFIN ts=" + DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() + ", nonce=" + nonce);
                request.Headers.Add("Accept", "application/vnd.yellowfin.api-v1+json");
                request.Content = new StringContent(
                    JsonConvert.SerializeObject(new { userName = restUsername, password = restPassword }),
                    System.Text.Encoding.UTF8,
                    "application/json"
                );
 
                HttpResponseMessage response = await client.SendAsync(request);
                string responseContent = await response.Content.ReadAsStringAsync();
 
                JObject jsonObject = JsonConvert.DeserializeObject<JObject>(responseContent);
                string accessToken = jsonObject["_embedded"]["accessToken"]["securityToken"].ToString();
 
                if (!string.IsNullOrEmpty(accessToken))
                {
                    Console.WriteLine("Access Token: " + accessToken);
                    return accessToken;
                }
                else
                {
                    Console.WriteLine("Token not retrieved successfully");
                    Environment.Exit(-1);
                }
            }
 
            return null; // Should not reach here due to Environment.Exit
        }
    }
}
Go
package main
 
import (
    "bytes"
    "encoding/json"
    "fmt"
    "io/ioutil"
    "math/rand"
    "net/http"
    "time"
)
 
func main() {
    host := "http://localhost:8080/Yellowfin"
    restUsername := "admin@yellowfin.com.au"
    restPassword := "test"
 
    userToLogin := "user1@yellowfin.com.au"
    userToLoginPassword := "test"
    entryDashboardUUID := "321e5a85-a349-4cfb-b8f4-c9141059a66a"
 
    createUserPayload := fmt.Sprintf(`{
        "signOnUser": {
            "userName": "%s",
            "password": "%s",
            "loginParameters": [
                "ENTRY=VIEWDASHBOARD",
                "DASHBOARDUUID=%s"
            ]
        }
    }`, userToLogin, userToLoginPassword, entryDashboardUUID)
 
    token, err := generateToken(host, restUsername, restPassword)
    if err != nil {
        fmt.Println("Error generating token:", err)
        return
    }
 
    fmt.Println("Payload:", createUserPayload)
 
    client := &http.Client{}
    req, err := http.NewRequest("POST", host+"/api/login-tokens", bytes.NewBuffer([]byte(createUserPayload)))
    if err != nil {
        fmt.Println("Error creating request:", err)
        return
    }
 
    nonce := rand.Int63()
 
    req.Header.Set("Authorization", fmt.Sprintf("YELLOWFIN ts=%d, nonce=%d, token=%s", time.Now().UnixMilli(), nonce, token))
    req.Header.Set("Accept", "application/vnd.yellowfin.api-v1+json")
    req.Header.Set("Content-Type", "application/json")
 
    resp, err := client.Do(req)
    if err != nil {
        fmt.Println("Error sending request:", err)
        return
    }
    defer resp.Body.Close()
 
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Println("Error reading response body:", err)
        return
    }
 
    var jsonObject map[string]interface{}
    err = json.Unmarshal(body, &jsonObject)
    if err != nil {
        fmt.Println("Error parsing JSON response:", err)
        return
    }
 
    securityToken, ok := jsonObject["securityToken"].(string)
    if !ok {
        fmt.Println("Token not retrieved successfully")
        return
    }
 
    fmt.Println("SSO Token:", securityToken)
}
 
func generateToken(host, restUsername, restPassword string) (string, error) {
    nonce := rand.Int63()
 
    requestBody, err := json.Marshal(map[string]string{
        "userName": restUsername,
        "password": restPassword,
    })
    if err != nil {
        fmt.Println("Error marshaling request body:", err)
        return "", err
    }
 
    client := &http.Client{}
 
    request, err := http.NewRequest("POST", host+"/api/refresh-tokens", bytes.NewBuffer(requestBody))
    if err != nil {
        fmt.Println("Error creating request:", err)
        return "", err
    }
 
    request.Header.Set("Authorization", fmt.Sprintf("YELLOWFIN ts=%d, nonce=%d", time.Now().UnixMilli(), nonce))
    request.Header.Set("Accept", "application/vnd.yellowfin.api-v1+json")
    request.Header.Set("Content-Type", "application/json")
 
    response, err := client.Do(request)
    if err != nil {
        fmt.Println("Error sending request:", err)
        return "", err
    }
    defer response.Body.Close()
 
    responseBody, err := ioutil.ReadAll(response.Body)
    if err != nil {
        fmt.Println("Error reading response body:", err)
        return "", err
    }
 
    var jsonResponse map[string]interface{}
    err = json.Unmarshal(responseBody, &jsonResponse)
    if err != nil {
        fmt.Println("Error parsing JSON response:", err)
        return "", err
    }
 
    accessToken, ok := jsonResponse["_embedded"].(map[string]interface{})["accessToken"].(map[string]interface{})["securityToken"].(string)
    if !ok {
        fmt.Println("Token not retrieved successfully")
        return "", fmt.Errorf("Token not retrieved successfully")
    }
 
    return accessToken, nil
}
JavaScript
const fetch = require("node-fetch");
 
async function main() {
    const host = "http://localhost:8080/Yellowfin";
    const restUsername = "admin@yellowfin.com.au";
    const restPassword = "test";
 
    const userToLogin = "user1@yellowfin.com.au";
    const userToLoginPassword = "test";
    const entryDashboardUUID = "321e5a85-a349-4cfb-b8f4-c9141059a66a";
 
    const createUserPayload = JSON.stringify({
        signOnUser: {
            userName: userToLogin,
            password: userToLoginPassword,
            loginParameters: [
                "ENTRY=VIEWDASHBOARD",
                `DASHBOARDUUID=${entryDashboardUUID}`
            ]
        }
    });
 
    const token = await generateToken(host, restUsername, restPassword);
 
    if (token === null) {
        console.error("Failed to retrieve access token");
        return;
    }
 
    console.log("Payload:", createUserPayload);
 
    const nonce = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
 
    const headers = {
        'Authorization': `YELLOWFIN ts=${Date.now()}, nonce=${nonce}, token=${token}`,
        'Accept': 'application/vnd.yellowfin.api-v1+json',
        'Content-Type': 'application/json'
    };
 
    try {
        const response = await fetch(`${host}/api/login-tokens`, {
            method: 'POST',
            headers: headers,
            body: createUserPayload
        });
 
        if (!response.ok) {
            throw new Error(`HTTP error! Status: ${response.status}`);
        }
 
        const jsonResponse = await response.json();
        const securityToken = jsonResponse.securityToken;
 
        if (securityToken) {
            console.log("SSO Token:", securityToken);
        } else {
            console.error("Failed to retrieve SSO token");
        }
    } catch (error) {
        console.error("Error:", error.message);
    }
}
 
async function generateToken(host, restUsername, restPassword) {
    const nonce = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
 
    const headers = {
        'Authorization': `YELLOWFIN ts=${Date.now()}, nonce=${nonce}`,
        'Accept': 'application/vnd.yellowfin.api-v1+json',
        'Content-Type': 'application/json'
    };
 
    const body = JSON.stringify({
        userName: restUsername,
        password: restPassword
    });
 
    try {
        const response = await fetch(`${host}/api/refresh-tokens`, {
            method: 'POST',
            headers: headers,
            body: body
        });
 
        if (!response.ok) {
            throw new Error(`HTTP error! Status: ${response.status}`);
        }
 
        const jsonResponse = await response.json();
        const accessToken = jsonResponse._embedded.accessToken.securityToken;
 
        if (accessToken) {
            console.log("Access Token:", accessToken);
        } else {
            console.error("Token not retrieved");
            return null;
        }
 
        return accessToken;
    } catch (error) {
        console.error("Error:", error.message);
        return null;
    }
}
 
main();
PHP
<?php
function main() {
    $host = "http://localhost:8080/Yellowfin";
    $restUsername = "admin@yellowfin.com.au";
    $restPassword = "test";
 
    $userToLogin = "user1@yellowfin.com.au";
    $userToLoginPassword = "test";
    $entryDashboardUUID = "321e5a85-a349-4cfb-b8f4-c9141059a66a";
 
    $createUserPayload = json_encode(array(
        "signOnUser" => array(
            "userName" => $userToLogin,
            "password" => $userToLoginPassword,
            "loginParameters" => [
                "ENTRY=VIEWDASHBOARD",
                "DASHBOARDUUID=$entryDashboardUUID"
            ]
        )
    ));
 
    try {
        $token = generateToken($host, $restUsername, $restPassword);
    } catch (Exception $e) {
        echo "Error generating token: " . $e->getMessage();
        return;
    }
 
    echo "Payload: " . $createUserPayload . "\n";
 
    $nonce = mt_rand();
    $headers = array(
        'Authorization: YELLOWFIN ts=' . intval(microtime(true) * 1000) . ', nonce=' . $nonce . ', token=' . $token,
        'Accept: application/vnd.yellowfin.api-v1+json',
        'Content-Type: application/json'
    );
 
    try {
        $response = httpRequest('POST', "$host/api/login-tokens", $headers, $createUserPayload);
        $jsonResponse = json_decode($response, true);
        if (isset($jsonResponse['securityToken'])) {
            echo "SSO Token: " . $jsonResponse['securityToken'] . "\n";
        } else {
            echo "Failed to retrieve SSO token\n";
        }
    } catch (Exception $e) {
        echo "Error sending request: " . $e->getMessage() . "\n";
    }
}
 
function generateToken($host, $restUsername, $restPassword) {
    $nonce = mt_rand();
 
    $requestBody = json_encode(array(
        "userName" => $restUsername,
        "password" => $restPassword
    ));
 
    $headers = array(
        'Authorization: YELLOWFIN ts=' . intval(microtime(true) * 1000) . ', nonce=' . $nonce,
        'Accept: application/vnd.yellowfin.api-v1+json',
        'Content-Type: application/json'
    );
 
    $response = httpRequest('POST', "$host/api/refresh-tokens", $headers, $requestBody);
 
    $jsonResponse = json_decode($response, true);
 
    if (isset($jsonResponse["_embedded"]["accessToken"]["securityToken"])) {
        $accessToken = $jsonResponse["_embedded"]["accessToken"]["securityToken"];
        echo "Access Token: " . $accessToken . "\n";
        return $accessToken;
    } else {
        throw new Exception("Token not retrieved successfully");
    }
}
 
function httpRequest($method, $url, $headers, $data = null) {
    $ch = curl_init();
 
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
 
    if ($data !== null) {
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
 
    $response = curl_exec($ch);
 
    if (curl_errno($ch)) {
        throw new Exception('Error: ' . curl_error($ch));
    }
 
    curl_close($ch);
 
    return $response;
}
 
main();
?>
Python
import json
import random
import time
 
import requests
 
def main():
    host = "http://localhost:8080/Yellowfin"
    rest_username = "admin@yellowfin.com.au"
    rest_password = "test"
 
    user_to_login = "user1@yellowfin.com.au"
    user_to_login_password = "test"
    entry_dashboard_UUID = "321e5a85-a349-4cfb-b8f4-c9141059a66a";
 
    create_user_payload = json.dumps({
        "signOnUser": {
            "userName": user_to_login,
            "password": user_to_login_password
        },
        "loginParameters": [
            "ENTRY=VIEWDASHBOARD",
            f'DASHBOARD_UUID={entry_dashboard_UUID}'
        ]
    })
 
    try:
        token = generate_token(host, rest_username, rest_password)
    except Exception as e:
        print(f"Error generating token: {e}")
        return
 
    print("Payload:", create_user_payload)
 
    nonce = random.randint(0, 2 ** 63 - 1)
 
    headers = {
        'Authorization': f'YELLOWFIN ts={int(time.time() * 1000)}, nonce={nonce}, token={token}',
        'Accept': 'application/vnd.yellowfin.api-v1+json',
        'Content-Type': 'application/json'
    }
 
    try:
        response = requests.post(host + "/api/login-tokens", headers=headers, data=create_user_payload)
        response.raise_for_status()
        json_response = response.json()
        security_token = json_response.get("securityToken")
        print("SSO Token:", security_token)
    except requests.RequestException as e:
        print(f"Error sending request: {e}")
 
 
def generate_token(host, rest_username, rest_password):
    nonce = random.randint(0, 2 ** 63 - 1)
 
    # Create request body
    request_body = json.dumps({
        "userName": rest_username,
        "password": rest_password
    })
 
    # Create request headers
    headers = {
        'Authorization': f'YELLOWFIN ts={int(time.time() * 1000)}, nonce={nonce}',
        'Accept': 'application/vnd.yellowfin.api-v1+json',
        'Content-Type': 'application/json'
    }
 
    # Send HTTP request
    response = requests.post(host + "/api/refresh-tokens", headers=headers, data=request_body)
 
    # Check response status
    if response.status_code == 200:
        # Parse JSON response
        json_response = response.json()
        access_token = json_response["_embedded"]["accessToken"]["securityToken"]
        print("Access Token:", access_token)
        return access_token
    else:
        raise Exception("Token not retrieved successfully")
 
if __name__ == "__main__":
    main()

ユーザーパスワードなしでSSOログイントークンを生成

SSO ペイロードのloginParametersでは、トークンが生成する新しいセッションのオプションを設定できます。noPassword=true を有効にすると、パスワードなしでユーザーのセッションを作成できます。

{
  "signOnUser": {
    "userName": "admin@yellowfin.com.au"
  },
  "noPassword": true
}

これは、ユーザーパスワードを保持する必要がないシナリオで使用できます。これには、新しいユーザーを作成するときにランダムなUUIDをパスワードとして割り当てることが含まれるかもしれません。ユーザーが常にSSOを介して (noPasswordを使用して) アプリケーションに入る場合は、実際のパスワードを知る必要はありません。

noPassword オプションを使用するには、Yellowfinの構成データベースで、Insecure ログインフラグを有効にする必要があります。

以下のコード例では、ユーザーのパスワードなしでセッショントークンを作成する方法を示しています。

Java
package rest.code.examples;
import java.io.IOException;
import java.util.Random;
import org.apache.hc.client5.http.fluent.Content;
import org.apache.hc.client5.http.fluent.Request;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
/**
 * Create a SSO login token without a user's password using the Yellowfin REST API
 */
public class CreateSSOTokenWithAccessTokenNoPassword {
    public static void main(String[] args) throws Exception {
 
        System.out.println("SSO Login User without a password");
 
        String host = "http://localhost:8080/Yellowfin";
        String restUsername = "admin@yellowfin.com.au";
        String restPassword = "test";
 
        String userToLogin = "user1@yellowfin.com.au";
 
        String createUserPayload = "{\n"
                + "  \"signOnUser\": {\n"
                + "    \"userName\": \""+ userToLogin + "\"\n"
                + "  },\n"
                + " \"noPassword\": true \n"
                + "}";
 
        String token = generateToken(host, restUsername, restPassword);
 
        System.out.println("Payload: " + createUserPayload);
 
        Content c = Request.post(host + "/api/login-tokens")
                .addHeader("Authorization", "YELLOWFIN ts=" + System.currentTimeMillis() + " , nonce=" + new Random().nextLong() + ", token=" + token)
                .addHeader("Accept", "application/vnd.yellowfin.api-v1+json")
                .addHeader("Content-Type", "application/json")
                .bodyString(createUserPayload, null)
                .execute().returnContent();
 
        JsonObject jsonObject = new JsonParser().parse(c.asString()).getAsJsonObject();
        JsonElement securityToken = jsonObject.get("securityToken");
 
        System.out.println("SSO Token: " + securityToken);
 
    }
 
    public static String generateToken(String host, String username, String password) throws IOException {
 
        Content c = Request.post(host + "/api/refresh-tokens")
                .addHeader("Authorization", "YELLOWFIN ts=" + System.currentTimeMillis() + " , nonce=" + new Random().nextLong())
                .addHeader("Accept", "application/vnd.yellowfin.api-v1+json")
                .addHeader("Content-Type", "application/json")
                .bodyString("{ \"userName\": \""+ username + "\",\"password\": \""+ password + "\"}", null)
                .execute().returnContent();
 
        JsonObject jsonObject = new JsonParser().parse(c.asString()).getAsJsonObject();
        JsonElement accessToken = jsonObject.getAsJsonObject("_embedded").getAsJsonObject("accessToken").get("securityToken");
 
        if (accessToken!=null) {
            System.out.println("Access Token: " + accessToken);
        } else {
            System.out.println("Token not retrieved successfully");
            System.exit(-1);
        }
        return accessToken.getAsString();
 
    }
 
}
C#
using System.Net.Http.Headers;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
 
namespace YellowfinAPIExamples
{
    public class CreateSSOTokenWithAccessTokenNoPassword
    {
        static async Task Main(string[] args)
        {
            string host = "http://localhost:8080/Yellowfin";
            string restUsername = "admin@yellowfin.com.au";
            string restPassword = "test";
            string userToLogin = "user1@yellowfin.com.au";
 
            string createUserPayload = "{\n"
                + "  \"signOnUser\": {\n"
                + "    \"userName\": \"" + userToLogin + "\",\n"
                + "  },\n"
                + "  \"noPassword\": true\n"
                + "}";
 
            string token = await GenerateToken(host, restUsername, restPassword);
 
            Console.WriteLine("Payload: " + createUserPayload);
 
            using (var httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("YELLOWFIN", "ts=" + DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() + " , nonce=" + new Random().NextInt64() + ", token=" + token);
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.yellowfin.api-v1+json"));
 
                var content = new StringContent(createUserPayload, System.Text.Encoding.UTF8, "application/json");
 
                HttpResponseMessage response = await httpClient.PostAsync(host + "/api/login-tokens", content);
                if (response.IsSuccessStatusCode)
                {
                    string responseBody = await response.Content.ReadAsStringAsync();
                    JObject jsonObject = JsonConvert.DeserializeObject<JObject>(responseBody);
                    string securityToken = jsonObject["securityToken"].ToString();
                    Console.WriteLine("SSO Token: " + securityToken);
                }
                else
                {
                    Console.WriteLine("Failed to create SSO token. Status code: " + response.StatusCode);
                }
            }
        }
 
        static async Task<string> GenerateToken(string host, string restUsername, string restPassword)
        {
            using (var client = new HttpClient())
            {
                long nonce = new Random().NextInt64();
 
                var request = new HttpRequestMessage(HttpMethod.Post, host + "/api/refresh-tokens");
                request.Headers.Add("Authorization", "YELLOWFIN ts=" + DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() + ", nonce=" + nonce);
                request.Headers.Add("Accept", "application/vnd.yellowfin.api-v1+json");
                request.Content = new StringContent(
                    JsonConvert.SerializeObject(new { userName = restUsername, password = restPassword }),
                    System.Text.Encoding.UTF8,
                    "application/json"
                );
 
                HttpResponseMessage response = await client.SendAsync(request);
                string responseContent = await response.Content.ReadAsStringAsync();
 
                JObject jsonObject = JsonConvert.DeserializeObject<JObject>(responseContent);
                string accessToken = jsonObject["_embedded"]["accessToken"]["securityToken"].ToString();
 
                if (!string.IsNullOrEmpty(accessToken))
                {
                    Console.WriteLine("Access Token: " + accessToken);
                    return accessToken;
                }
                else
                {
                    Console.WriteLine("Token not retrieved successfully");
                    Environment.Exit(-1);
                }
            }
 
            return null; // Should not reach here due to Environment.Exit
        }
    }
}
Go
package main
 
import (
    "bytes"
    "encoding/json"
    "fmt"
    "io/ioutil"
    "math/rand"
    "net/http"
    "time"
)
 
func main() {
    host := "http://localhost:8080/Yellowfin"
    restUsername := "admin@yellowfin.com.au"
    restPassword := "test"
 
    userToLogin := "user1@yellowfin.com.au"
 
    createUserPayload := fmt.Sprintf(`{
        "signOnUser": {
            "userName": "%s"
        },
        "noPassword": true
    }`, userToLogin)
     
    token, err := generateToken(host, restUsername, restPassword)
    if err != nil {
        fmt.Println("Error generating token:", err)
        return
    }
 
    fmt.Println("Payload:", createUserPayload)
 
    client := &http.Client{}
    req, err := http.NewRequest("POST", host+"/api/login-tokens", bytes.NewBuffer([]byte(createUserPayload)))
    if err != nil {
        fmt.Println("Error creating request:", err)
        return
    }
 
    nonce := rand.Int63()
 
    req.Header.Set("Authorization", fmt.Sprintf("YELLOWFIN ts=%d, nonce=%d, token=%s", time.Now().UnixMilli(), nonce, token))
    req.Header.Set("Accept", "application/vnd.yellowfin.api-v1+json")
    req.Header.Set("Content-Type", "application/json")
 
    resp, err := client.Do(req)
    if err != nil {
        fmt.Println("Error sending request:", err)
        return
    }
    defer resp.Body.Close()
 
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Println("Error reading response body:", err)
        return
    }
 
    var jsonObject map[string]interface{}
    err = json.Unmarshal(body, &jsonObject)
    if err != nil {
        fmt.Println("Error parsing JSON response:", err)
        return
    }
 
    securityToken, ok := jsonObject["securityToken"].(string)
    if !ok {
        fmt.Println("Token not retrieved successfully")
        return
    }
 
    fmt.Println("SSO Token:", securityToken)
}
 
func generateToken(host, restUsername, restPassword string) (string, error) {
    nonce := rand.Int63()
 
    requestBody, err := json.Marshal(map[string]string{
        "userName": restUsername,
        "password": restPassword,
    })
    if err != nil {
        fmt.Println("Error marshaling request body:", err)
        return "", err
    }
 
    client := &http.Client{}
 
    request, err := http.NewRequest("POST", host+"/api/refresh-tokens", bytes.NewBuffer(requestBody))
    if err != nil {
        fmt.Println("Error creating request:", err)
        return "", err
    }
 
    request.Header.Set("Authorization", fmt.Sprintf("YELLOWFIN ts=%d, nonce=%d", time.Now().UnixMilli(), nonce))
    request.Header.Set("Accept", "application/vnd.yellowfin.api-v1+json")
    request.Header.Set("Content-Type", "application/json")
 
    response, err := client.Do(request)
    if err != nil {
        fmt.Println("Error sending request:", err)
        return "", err
    }
    defer response.Body.Close()
 
    responseBody, err := ioutil.ReadAll(response.Body)
    if err != nil {
        fmt.Println("Error reading response body:", err)
        return "", err
    }
 
    var jsonResponse map[string]interface{}
    err = json.Unmarshal(responseBody, &jsonResponse)
    if err != nil {
        fmt.Println("Error parsing JSON response:", err)
        return "", err
    }
 
    accessToken, ok := jsonResponse["_embedded"].(map[string]interface{})["accessToken"].(map[string]interface{})["securityToken"].(string)
    if !ok {
        fmt.Println("Token not retrieved successfully")
        return "", fmt.Errorf("Token not retrieved successfully")
    }
 
    return accessToken, nil
}
JavaScript
const fetch = require("node-fetch");
 
async function main() {
    const host = "http://localhost:8080/Yellowfin";
    const restUsername = "admin@yellowfin.com.au";
    const restPassword = "test";
 
    const userToLogin = "user1@yellowfin.com.au";
    const userToLoginPassword = "test";
 
    const createUserPayload = JSON.stringify({
        signOnUser: {
            userName: userToLogin,
            password: userToLoginPassword
        },
        noPassword: true
    });
 
    const token = await generateToken(host, restUsername, restPassword);
 
    if (token === null) {
        console.error("Failed to retrieve access token");
        return;
    }
 
    console.log("Payload:", createUserPayload);
 
    const nonce = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
 
    const headers = {
        'Authorization': `YELLOWFIN ts=${Date.now()}, nonce=${nonce}, token=${token}`,
        'Accept': 'application/vnd.yellowfin.api-v1+json',
        'Content-Type': 'application/json'
    };
 
    try {
        const response = await fetch(`${host}/api/login-tokens`, {
            method: 'POST',
            headers: headers,
            body: createUserPayload
        });
 
        if (!response.ok) {
            throw new Error(`HTTP error! Status: ${response.status}`);
        }
 
        const jsonResponse = await response.json();
        const securityToken = jsonResponse.securityToken;
 
        if (securityToken) {
            console.log("SSO Token:", securityToken);
        } else {
            console.error("Failed to retrieve SSO token");
        }
    } catch (error) {
        console.error("Error:", error.message);
    }
}
 
async function generateToken(host, restUsername, restPassword) {
    const nonce = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
 
    const headers = {
        'Authorization': `YELLOWFIN ts=${Date.now()}, nonce=${nonce}`,
        'Accept': 'application/vnd.yellowfin.api-v1+json',
        'Content-Type': 'application/json'
    };
 
    const body = JSON.stringify({
        userName: restUsername,
        password: restPassword
    });
 
    try {
        const response = await fetch(`${host}/api/refresh-tokens`, {
            method: 'POST',
            headers: headers,
            body: body
        });
 
        if (!response.ok) {
            throw new Error(`HTTP error! Status: ${response.status}`);
        }
 
        const jsonResponse = await response.json();
        const accessToken = jsonResponse._embedded.accessToken.securityToken;
 
        if (accessToken) {
            console.log("Access Token:", accessToken);
        } else {
            console.error("Token not retrieved");
            return null;
        }
 
        return accessToken;
    } catch (error) {
        console.error("Error:", error.message);
        return null;
    }
}
 
main();
PHP
<?php
function main() {
    $host = "http://localhost:8080/Yellowfin";
    $restUsername = "admin@yellowfin.com.au";
    $restPassword = "test";
 
    $userToLogin = "user1@yellowfin.com.au";
    $userToLoginPassword = "test";
 
    $createUserPayload = json_encode(array(
        "signOnUser" => array(
            "userName" => $userToLogin,
        ),
        "noPassword" => true
    ));
 
    try {
        $token = generateToken($host, $restUsername, $restPassword);
    } catch (Exception $e) {
        echo "Error generating token: " . $e->getMessage();
        return;
    }
 
    echo "Payload: " . $createUserPayload . "\n";
 
    $nonce = mt_rand();
    $headers = array(
        'Authorization: YELLOWFIN ts=' . intval(microtime(true) * 1000) . ', nonce=' . $nonce . ', token=' . $token,
        'Accept: application/vnd.yellowfin.api-v1+json',
        'Content-Type: application/json'
    );
 
    try {
        $response = httpRequest('POST', "$host/api/login-tokens", $headers, $createUserPayload);
        $jsonResponse = json_decode($response, true);
        if (isset($jsonResponse['securityToken'])) {
            echo "SSO Token: " . $jsonResponse['securityToken'] . "\n";
        } else {
            echo "Failed to retrieve SSO token\n";
        }
    } catch (Exception $e) {
        echo "Error sending request: " . $e->getMessage() . "\n";
    }
}
 
function generateToken($host, $restUsername, $restPassword) {
    $nonce = mt_rand();
 
    $requestBody = json_encode(array(
        "userName" => $restUsername,
        "password" => $restPassword
    ));
 
    $headers = array(
        'Authorization: YELLOWFIN ts=' . intval(microtime(true) * 1000) . ', nonce=' . $nonce,
        'Accept: application/vnd.yellowfin.api-v1+json',
        'Content-Type: application/json'
    );
 
    $response = httpRequest('POST', "$host/api/refresh-tokens", $headers, $requestBody);
 
    $jsonResponse = json_decode($response, true);
 
    if (isset($jsonResponse["_embedded"]["accessToken"]["securityToken"])) {
        $accessToken = $jsonResponse["_embedded"]["accessToken"]["securityToken"];
        echo "Access Token: " . $accessToken . "\n";
        return $accessToken;
    } else {
        throw new Exception("Token not retrieved successfully");
    }
}
 
function httpRequest($method, $url, $headers, $data = null) {
    $ch = curl_init();
 
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
 
    if ($data !== null) {
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
 
    $response = curl_exec($ch);
 
    if (curl_errno($ch)) {
        throw new Exception('Error: ' . curl_error($ch));
    }
 
    curl_close($ch);
 
    return $response;
}
 
main();
?>
Python
import json
import random
import time
 
import requests
 
def main():
    host = "http://localhost:8080/Yellowfin"
    rest_username = "admin@yellowfin.com.au"
    rest_password = "test"
 
    user_to_login = "user1@yellowfin.com.au"
 
    create_user_payload = json.dumps({
        "signOnUser": {
            "userName": user_to_login,
        },
        "noPassword": True
    })
 
    try:
        token = generate_token(host, rest_username, rest_password)
    except Exception as e:
        print(f"Error generating token: {e}")
        return
 
    print("Payload:", create_user_payload)
 
    nonce = random.randint(0, 2 ** 63 - 1)
 
    headers = {
        'Authorization': f'YELLOWFIN ts={int(time.time() * 1000)}, nonce={nonce}, token={token}',
        'Accept': 'application/vnd.yellowfin.api-v1+json',
        'Content-Type': 'application/json'
    }
 
    try:
        response = requests.post(host + "/api/login-tokens", headers=headers, data=create_user_payload)
        response.raise_for_status()
        json_response = response.json()
        security_token = json_response.get("securityToken")
        print("SSO Token:", security_token)
    except requests.RequestException as e:
        print(f"Error sending request: {e}")
 
 
def generate_token(host, rest_username, rest_password):
    nonce = random.randint(0, 2 ** 63 - 1)
 
    # Create request body
    request_body = json.dumps({
        "userName": rest_username,
        "password": rest_password
    })
 
    # Create request headers
    headers = {
        'Authorization': f'YELLOWFIN ts={int(time.time() * 1000)}, nonce={nonce}',
        'Accept': 'application/vnd.yellowfin.api-v1+json',
        'Content-Type': 'application/json'
    }
 
    # Send HTTP request
    response = requests.post(host + "/api/refresh-tokens", headers=headers, data=request_body)
 
    # Check response status
    if response.status_code == 200:
        # Parse JSON response
        json_response = response.json()
        access_token = json_response["_embedded"]["accessToken"]["securityToken"]
        print("Access Token:", access_token)
        return access_token
    else:
        raise Exception("Token not retrieved successfully")
 
if __name__ == "__main__":
    main()

SSOログアウトの実行 / SSOで作成されたセッションの破棄

SSOを介して作成されたセッションは、DELETE /api/login-tokens/{tokenId} エンドポイント Delete Login Tokenを使用して破棄できます。{tokenId}が最初のSSO ログインREST 呼び出しによって返されたtokenIdの場合。

tokenIdは、SSO セッションを初期化するために使用されるsecurityTokenとは異なります。tokenIdは、後でセッションを終了するための容量を必要とするアプリケーションによって格納される必要があります。

以下のコード例では、特定のtokenIdを使用して、SSO ログインで作成された既存のセッションを破棄する方法を示しています。

Java
package rest.code.examples;
import java.io.IOException;
import java.util.Random;
import org.apache.hc.client5.http.fluent.Content;
import org.apache.hc.client5.http.fluent.Request;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
/**
 * Destroy a SSO session using the Yellowfin REST API
 */
public class DestroySSOSession {
    public static void main(String[] args) throws Exception {
 
        String host = "http://localhost:8080/Yellowfin";
        String restUsername = "admin@yellowfin.com.au";
        String restPassword = "test";
 
        // This is a loginTokenId from the initial REST call to receive a securityToken for SSO
        String loginTokenId = "ac69b491-26cc-c399-7e59-2e441c9e1433";
 
        String token = generateToken(host, restUsername, restPassword);
 
        Content c = Request.delete(host + "/api/login-tokens/" + loginTokenId)
                .addHeader("Authorization", "YELLOWFIN ts=" + System.currentTimeMillis() + " , nonce=" + new Random().nextLong() + ", token=" + token)
                .addHeader("Accept", "application/vnd.yellowfin.api-v1+json")
                .addHeader("Content-Type", "application/json")
                .execute().returnContent();
        System.out.println(c.toString());
 
    }
 
    public static String generateToken(String host, String username, String password) throws IOException {
 
        Content c = Request.post(host + "/api/refresh-tokens")
                .addHeader("Authorization", "YELLOWFIN ts=" + System.currentTimeMillis() + " , nonce=" + new Random().nextLong())
                .addHeader("Accept", "application/vnd.yellowfin.api-v1+json")
                .addHeader("Content-Type", "application/json")
                .bodyString("{ \"userName\": \""+ username + "\",\"password\": \""+ password + "\"}", null)
                .execute().returnContent();
 
        JsonObject jsonObject = new JsonParser().parse(c.asString()).getAsJsonObject();
        JsonElement accessToken = jsonObject.getAsJsonObject("_embedded").getAsJsonObject("accessToken").get("securityToken");
 
        if (accessToken!=null) {
            System.out.println("Access Token: " + accessToken);
        } else {
            System.out.println("Token not retrieved successfully");
            System.exit(-1);
        }
        return accessToken.getAsString();
 
    }
 
}
C#
using System.Net.Http.Headers;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
 
namespace YellowfinAPIExamples
{
    public class DestroySSOSession
    {
        public static async Task Main(string[] args)
        {
            string host = "http://localhost:8080/Yellowfin";
            string restUsername = "admin@yellowfin.com.au";
            string restPassword = "test";
            string loginTokenId = "ac69b491-26cc-c399-7e59-2e441c9e1433";
 
            string token = await GenerateToken(host, restUsername, restPassword);
 
            Console.WriteLine("Deleting login token: " + loginTokenId);
 
            using (var httpClient = new HttpClient())
            {
                long nonce = new Random().NextInt64();
 
                // Create DELETE request
                var request = new HttpRequestMessage(HttpMethod.Delete, host + "/api/login-tokens/" + loginTokenId);
                request.Headers.Authorization = new AuthenticationHeaderValue("YELLOWFIN", $"ts={DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}, nonce={nonce}, token={token}");
                request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.yellowfin.api-v1+json"));
 
                HttpResponseMessage response = await httpClient.SendAsync(request);
 
                if (response.IsSuccessStatusCode)
                {
                    string responseBody = await response.Content.ReadAsStringAsync();
                    Console.WriteLine(responseBody);
                }
                else
                {
                    Console.WriteLine("Failed to delete login token. Status code: " + response.StatusCode);
                }
            }
        }
 
        public static async Task<string> GenerateToken(string host, string username, string password)
        {
            using (var httpClient = new HttpClient())
            {
                long nonce = new Random().NextInt64();
 
                // Create POST request
                var request = new HttpRequestMessage(HttpMethod.Post, host + "/api/refresh-tokens");
                request.Headers.Authorization = new AuthenticationHeaderValue("YELLOWFIN", $"ts={DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}, nonce={nonce}");
                request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.yellowfin.api-v1+json"));
                request.Content = new StringContent(JsonConvert.SerializeObject(new { userName = username, password = password }));
                request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
 
                // Send request and get response
                HttpResponseMessage response = await httpClient.SendAsync(request);
                string responseContent = await response.Content.ReadAsStringAsync();
 
                // Parse JSON response
                JObject jsonObject = JsonConvert.DeserializeObject<JObject>(responseContent);
                string accessToken = jsonObject["_embedded"]["accessToken"]["securityToken"].ToString();
 
                if (!string.IsNullOrEmpty(accessToken))
                {
                    Console.WriteLine("Access Token: " + accessToken);
                }
                else
                {
                    Console.WriteLine("Token not retrieved");
                    Environment.Exit(-1);
                }
 
                return accessToken;
            }
        }
    }
}
Go
package main
 
import (
    "bytes"
    "encoding/json"
    "fmt"
    "io/ioutil"
    "math/rand"
    "net/http"
    "time"
)
 
func main() {
    host := "http://localhost:8080/Yellowfin"
    restUsername := "admin@yellowfin.com.au"
    restPassword := "test"
    loginTokenID := "ac69b491-26cc-c399-7e59-2e441c9e1433"
 
    token, err := generateToken(host, restUsername, restPassword)
    if err != nil {
        fmt.Println("Error generating token:", err)
        return
    }
 
    fmt.Println("Deleting login token:", loginTokenID)
 
    client := &http.Client{}
    req, err := http.NewRequest("DELETE", host+"/api/login-tokens/"+loginTokenID, nil)
    if err != nil {
        fmt.Println("Error creating request:", err)
        return
    }
 
    nonce := rand.Int63()
 
    req.Header.Set("Authorization", fmt.Sprintf("YELLOWFIN ts=%d, nonce=%d, token=%s", time.Now().UnixMilli(), nonce, token))
    req.Header.Set("Accept", "application/vnd.yellowfin.api-v1+json")
    req.Header.Set("Content-Type", "application/json")
 
    resp, err := client.Do(req)
    if err != nil {
        fmt.Println("Error sending request:", err)
        return
    }
    defer resp.Body.Close()
 
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Println("Error reading response body:", err)
        return
    }
 
    fmt.Println(string(body))
}
 
func generateToken(host, username, password string) (string, error) {
    // Generate nonce
    nonce := rand.Int63()
 
    // Create request body
    requestBody, err := json.Marshal(map[string]string{
        "userName": username,
        "password": password,
    })
    if err != nil {
        fmt.Println("Error marshaling request body:", err)
        return "", err
    }
 
    // Create HTTP client
    client := &http.Client{}
 
    // Create HTTP request
    request, err := http.NewRequest("POST", host+"/api/refresh-tokens", bytes.NewBuffer(requestBody))
    if err != nil {
        fmt.Println("Error creating request:", err)
        return "", err
    }
 
    // Add request headers
    request.Header.Set("Authorization", fmt.Sprintf("YELLOWFIN ts=%d, nonce=%d", time.Now().UnixMilli(), nonce))
    request.Header.Set("Accept", "application/vnd.yellowfin.api-v1+json")
    request.Header.Set("Content-Type", "application/json")
 
    // Send HTTP request
    response, err := client.Do(request)
    if err != nil {
        fmt.Println("Error sending request:", err)
        return "", err
    }
    defer response.Body.Close()
 
    // Read response body
    responseBody, err := ioutil.ReadAll(response.Body)
    if err != nil {
        fmt.Println("Error reading response body:", err)
        return "", err
    }
 
    // Parse JSON response
    var jsonResponse map[string]interface{}
    err = json.Unmarshal(responseBody, &jsonResponse)
    if err != nil {
        fmt.Println("Error parsing JSON response:", err)
        return "", err
    }
 
    // Get access token from response
    accessToken, ok := jsonResponse["_embedded"].(map[string]interface{})["accessToken"].(map[string]interface{})["securityToken"].(string)
    if !ok {
        fmt.Println("Token not retrieved")
        return "", fmt.Errorf("Token not retrieved successfully")
    }
 
    return accessToken, nil
}
JavaScript
const fetch = require("node-fetch");
 
async function main() {
    const host = "http://localhost:8080/Yellowfin";
    const restUsername = "admin@yellowfin.com.au";
    const restPassword = "test";
 
    const loginTokenId = "ac69b491-26cc-c399-7e59-2e441c9e1433";
 
    try {
        const token = await generateToken(host, restUsername, restPassword);
 
        const nonce = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
 
        const headers = {
            'Authorization': `YELLOWFIN ts=${Date.now()}, nonce=${nonce}, token=${token}`,
            'Accept': 'application/vnd.yellowfin.api-v1+json',
            'Content-Type': 'application/json'
        };
 
        const response = await fetch(`${host}/api/login-tokens/${loginTokenId}`, {
            method: 'DELETE',
            headers: headers
        });
 
        if (!response.ok) {
            throw new Error(`HTTP error! Status: ${response.status}`);
        }
 
        const responseBody = await response.text();
        console.log(responseBody);
    } catch (error) {
        console.error("Error:", error.message);
    }
}
 
async function generateToken(host, username, password) {
    const nonce = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
 
    const headers = {
        'Authorization': `YELLOWFIN ts=${Date.now()}, nonce=${nonce}`,
        'Accept': 'application/vnd.yellowfin.api-v1+json',
        'Content-Type': 'application/json'
    };
 
    const body = JSON.stringify({
        userName: username,
        password: password
    });
 
    try {
        const response = await fetch(`${host}/api/refresh-tokens`, {
            method: 'POST',
            headers: headers,
            body: body
        });
 
        if (!response.ok) {
            throw new Error(`HTTP error! Status: ${response.status}`);
        }
 
        const jsonResponse = await response.json();
        const accessToken = jsonResponse._embedded.accessToken.securityToken;
 
        if (accessToken) {
            console.log(`Access Token: ${accessToken}`);
            return accessToken;
        } else {
            throw new Error("Token not retrieved successfully");
        }
    } catch (error) {
        console.error("Error:", error.message);
        return null;
    }
}
 
main();
PHP
<?php
function main() {
    $host = "http://localhost:8080/Yellowfin";
    $restUsername = "admin@yellowfin.com.au";
    $restPassword = "test";
 
    $loginTokenId = "ac69b491-26cc-c399-7e59-2e441c9e1433";
 
    try {
        $token = generateToken($host, $restUsername, $restPassword);
    } catch (Exception $e) {
        echo "Error generating token: " . $e->getMessage();
        return;
    }
 
    $nonce = mt_rand();
    $timestamp = intval(microtime(true) * 1000);
 
    $headers = array(
        'Authorization: YELLOWFIN ts=' . $timestamp . ', nonce=' . $nonce . ', token=' . $token,
        'Accept: application/vnd.yellowfin.api-v1+json',
        'Content-Type: application/json'
    );
 
    try {
        $response = httpRequest('DELETE', "$host/api/login-tokens/$loginTokenId", $headers);
        echo $response;
    } catch (Exception $e) {
        echo "Error sending request: " . $e->getMessage();
    }
}
 
function generateToken($host, $restUsername, $restPassword) {
    $nonce = mt_rand();
    $timestamp = intval(microtime(true) * 1000);
 
    $requestBody = json_encode(array(
        "userName" => $restUsername,
        "password" => $restPassword
    ));
 
    $headers = array(
        'Authorization: YELLOWFIN ts=' . $timestamp . ', nonce=' . $nonce,
        'Accept: application/vnd.yellowfin.api-v1+json',
        'Content-Type: application/json'
    );
 
    $response = httpRequest('POST', "$host/api/refresh-tokens", $headers, $requestBody);
    $jsonResponse = json_decode($response, true);
 
    if (isset($jsonResponse["_embedded"]["accessToken"]["securityToken"])) {
        $accessToken = $jsonResponse["_embedded"]["accessToken"]["securityToken"];
        echo "Access Token: " . $accessToken;
 
        return $accessToken;
    } else {
        throw new Exception("Token not retrieved successfully");
    }
}
 
function httpRequest($method, $url, $headers, $data = null) {
    $ch = curl_init();
 
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
 
    if ($data !== null) {
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
 
    $response = curl_exec($ch);
 
    if (curl_errno($ch)) {
        throw new Exception('Error: ' . curl_error($ch));
    }
 
    curl_close($ch);
 
    return $response;
}
 
main();
?>
Python
import json
import random
import time
 
import requests
 
def main():
    host = "http://localhost:8080/Yellowfin"
    rest_username = "admin@yellowfin.com.au"
    rest_password = "test"
 
    login_token_id = "ac69b491-26cc-c399-7e59-2e441c9e1433"
 
    try:
        token = generate_token(host, rest_username, rest_password)
    except Exception as e:
        print(f"Error generating token: {e}")
        return
 
    nonce = random.randint(0, 2 ** 63 - 1)
 
    headers = {
        'Authorization': f'YELLOWFIN ts={int(time.time() * 1000)}, nonce={nonce}, token={token}',
        'Accept': 'application/vnd.yellowfin.api-v1+json',
        'Content-Type': 'application/json'
    }
 
    try:
        response = requests.delete(f'{host}/api/login-tokens/{login_token_id}', headers=headers)
        response.raise_for_status()
        print(response.text)
    except requests.RequestException as e:
        print(f"Error sending request: {e}")
 
def generate_token(host, username, password):
    nonce = random.randint(0, 2 ** 63 - 1)
 
    request_body = json.dumps({
        "userName": username,
        "password": password
    })
 
    headers = {
        'Authorization': f'YELLOWFIN ts={int(time.time() * 1000)}, nonce={nonce}',
        'Accept': 'application/vnd.yellowfin.api-v1+json',
        'Content-Type': 'application/json'
    }
 
    response = requests.post(f'{host}/api/refresh-tokens', headers=headers, data=request_body)
 
    if response.status_code == 200:
        json_response = response.json()
        access_token = json_response["_embedded"]["accessToken"]["securityToken"]
        print("Access Token:", access_token)
        return access_token
    else:
        raise Exception("Token not retrieved successfully")
 
if __name__ == "__main__":
    main()

SSOでのJWTトークンの使用

Yellowfinには、認証にJWTトークンを使用するメカニズムが組み込まれています。これは、REST APIを直接使用するよりも柔軟性に欠けますが、SSOの実装 (およびオンボーディング) を非常に簡単に実現できます。

JWTトークンは、ユーザーをYellowfinにSSOするためのすべての情報を保持します。ユーザーが既に作成されている場合は、JWTペイロードにuserIdを含めるだけでSSOを実現できます。JWT メソッドでは、ユーザーが存在しない場合にユーザーをオンボードすることもできますが、新しいユーザーの作成に使用できるように、JWTペイロードに追加情報 (名前や電子メールアドレス) も指定する必要があります。

YellowfinのJWT 実装は、ペイロードの安全性を確保するために、複数の署名および暗号化方式をサポートしています。

JWTは、システム構成ページの認証タブで有効にできます。ここでは、JWT ペイロードとYellowfinが必要とするデータとの間の属性のマッピングを指定できます。

JWTシングルサインオンのトグルを切り替えることで、JWT機能を有効にします。

JWTトークンからYellowfinが必要とする属性へのマッピングを提供します。

カスタムJWTトークンを作成する場合、アプリケーションコードはJWTトークンを生成し、サポートされているアルゴリズムで署名または暗号化する必要があります。こちらのウェブサイト https://jwt.io/ では、テスト用に手動でJWTトークンを作成するためのUIを提供しています。

jwt.io からのスクリーンショットは、右側にペイロードプロパティを、左側に結果のJWTトークンを示しています。

左のトークンは、以下のURLからYellowfinに渡すことができます。これは、SSOトークンと同様に動作し、ユーザー名/パスワードをバイパスしてユーザーをYellowfinにログインさせます。

http://<yellowfin-server-address>/JWTLogin.i4?jwtToken=<jwt-token>

JWT オンボーディング (新規ユーザー作成) も有効にすることができ、JWTトークン内で定義されたユーザーがYellowfinに存在しない場合 (提供されたユーザーIDに基づいて) 新しいユーザーを作成します。この機能が有効になっている場合、JWTトークンには、ユーザー作成に必要な属性が含まれている必要があります。