Skip to end of metadata
Go to start of metadata

You are viewing an old version of this content. View the current version.

Compare with Current View Version History

« Previous Version 7 Next »

Wir unterstützen als Client Authentifizeriung die Private Key JWT-Methode. D.h. damit du die Public API von Micromate aufrufen kannst, musst du ein JWT erstellen und dieses mit dem Schlüssel welchen du von uns erhalten hast signieren.

Falls du noch keinen Schlüssel erhalten hast, melde dich auf hello@micromate.ai.

Zum signieren des JWT verwendest du am besten eine bestehende Bibliothek:

Technologie

Library

NodeJS

jsonwebtoken

PHP

openssl_pkey_get_private

Beispiel NodeJS

Folgend eine Beispielimplementation in Typescript verwendet in einer NodeJS-Umgebung:

import * as jsonwebtoken from 'jsonwebtoken';
import fs from "fs";
import {JWTPrivateKey} from "./JWTPrivateKey";
import axios from "axios";

   
export interface JWTPrivateKey {
    type: string,
    keyId: string,    
    key: string,
    userId: string
}
   
public static createSignedJWT(): string {
    // Read the private key
    const rawPrivateKey = fs.readFileSync('./privateKey.json', 'utf8');
    const privateKey = JSON.parse(rawPrivateKey) as JWTPrivateKey;
    
    // Valid from now minus 10 seconds
    const validFrom: number = Math.floor(Date.now() / 1000) - 10;
    
    // Expiration 1 hour after now
    const expiration: number = Math.floor(Date.now() / 1000) + (60 * 60);
    
    // Create a JWT token
    const jwtToSign = {
        iss: privateKey.userId,
        sub: privateKey.userId,
        aud: 'https://micromate-q4ee42.zitadel.cloud',
        iat: validFrom,
        exp: expiration
    };
    
    // Sign and return the token
    return jsonwebtoken.sign(jwtToSign, privateKey.key, {algorithm: 'RS256', keyid: privateKey.keyId});
}

public static async getOAuthToken(): Promise<string> {
    // Create signed token
    const token = createSignedJWT();
    
    // Request to token endpoint to get an access token
    // which then can be used to access the micromate public api
    const data = await axios.post<{ token_type: string, access_token: string, expires_in: number }>('https://login.micromate.ai/oauth/v2/token', {}, {
        params: {
            'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer',
            'scope': 'openid profile email urn:zitadel:iam:org:project:id:69234237810729019:aud',
            'assertion': token
        },
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    });
    
    // Return access token
    return data.data.access_token;
}

// Call Micromate Public API
const token = await getOAuthToken();
const data = await axios.get('http://bot.micromate.ai/api/public/organization/{organizationId}/learningPackage/{learningPackageId}/questions', {
    headers: {Authorization: `Bearer ${token}`}
});
  • No labels