first commit
This commit is contained in:
494
apps/docs/lib/auth-examples.ts
Normal file
494
apps/docs/lib/auth-examples.ts
Normal file
@@ -0,0 +1,494 @@
|
||||
import type { CodeExample } from '@/lib/code-example';
|
||||
|
||||
const API_BASE = 'https://id.lendry.ru';
|
||||
|
||||
const loginBody = `{
|
||||
"login": "user@example.com",
|
||||
"password": "SecurePass123",
|
||||
"fingerprint": "device-fp-uuid",
|
||||
"deviceName": "Chrome на Windows",
|
||||
"deviceType": "WEB"
|
||||
}`;
|
||||
|
||||
export const authLoginExamples: CodeExample[] = [
|
||||
{
|
||||
id: 'curl',
|
||||
label: 'cURL',
|
||||
language: 'bash',
|
||||
code: `curl -X POST ${API_BASE}/auth/login/password \\
|
||||
-H "Content-Type: application/json" \\
|
||||
-d '${loginBody.replace(/\n/g, '\n ')}'`
|
||||
},
|
||||
{
|
||||
id: 'javascript',
|
||||
label: 'JavaScript',
|
||||
language: 'javascript',
|
||||
code: `const response = await fetch('${API_BASE}/auth/login/password', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
login: 'user@example.com',
|
||||
password: 'SecurePass123',
|
||||
fingerprint: crypto.randomUUID(),
|
||||
deviceName: 'Chrome на Windows',
|
||||
deviceType: 'WEB'
|
||||
})
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(await response.text());
|
||||
}
|
||||
|
||||
const session = await response.json();
|
||||
// session.accessToken, session.refreshToken, session.requiresPin`
|
||||
},
|
||||
{
|
||||
id: 'typescript',
|
||||
label: 'TypeScript',
|
||||
language: 'typescript',
|
||||
code: `interface LoginPayload {
|
||||
login: string;
|
||||
password: string;
|
||||
fingerprint: string;
|
||||
deviceName: string;
|
||||
deviceType: 'WEB' | 'MOBILE' | 'DESKTOP';
|
||||
}
|
||||
|
||||
interface AuthTokens {
|
||||
accessToken: string;
|
||||
refreshToken: string;
|
||||
requiresPin?: boolean;
|
||||
}
|
||||
|
||||
export async function login(payload: LoginPayload): Promise<AuthTokens> {
|
||||
const response = await fetch('${API_BASE}/auth/login/password', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(payload)
|
||||
});
|
||||
if (!response.ok) throw new Error(await response.text());
|
||||
return response.json();
|
||||
}`
|
||||
},
|
||||
{
|
||||
id: 'python',
|
||||
label: 'Python',
|
||||
language: 'python',
|
||||
code: `import requests
|
||||
|
||||
response = requests.post(
|
||||
'${API_BASE}/auth/login/password',
|
||||
json={
|
||||
'login': 'user@example.com',
|
||||
'password': 'SecurePass123',
|
||||
'fingerprint': 'device-fp-uuid',
|
||||
'deviceName': 'Chrome на Windows',
|
||||
'deviceType': 'WEB',
|
||||
},
|
||||
timeout=15,
|
||||
)
|
||||
response.raise_for_status()
|
||||
session = response.json()
|
||||
print(session['accessToken'])`
|
||||
},
|
||||
{
|
||||
id: 'php',
|
||||
label: 'PHP',
|
||||
language: 'php',
|
||||
code: `<?php
|
||||
$payload = json_encode([
|
||||
'login' => 'user@example.com',
|
||||
'password' => 'SecurePass123',
|
||||
'fingerprint' => bin2hex(random_bytes(16)),
|
||||
'deviceName' => 'Chrome на Windows',
|
||||
'deviceType' => 'WEB',
|
||||
]);
|
||||
|
||||
$ch = curl_init('${API_BASE}/auth/login/password');
|
||||
curl_setopt_array($ch, [
|
||||
CURLOPT_POST => true,
|
||||
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
|
||||
CURLOPT_POSTFIELDS => $payload,
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
]);
|
||||
$response = curl_exec($ch);
|
||||
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
if ($status >= 400) {
|
||||
throw new RuntimeException($response);
|
||||
}
|
||||
|
||||
$session = json_decode($response, true);`
|
||||
},
|
||||
{
|
||||
id: 'go',
|
||||
label: 'Go',
|
||||
language: 'go',
|
||||
code: `package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type LoginRequest struct {
|
||||
Login string \`json:"login"\`
|
||||
Password string \`json:"password"\`
|
||||
Fingerprint string \`json:"fingerprint"\`
|
||||
DeviceName string \`json:"deviceName"\`
|
||||
DeviceType string \`json:"deviceType"\`
|
||||
}
|
||||
|
||||
func login() (map[string]any, error) {
|
||||
body, _ := json.Marshal(LoginRequest{
|
||||
Login: "user@example.com", Password: "SecurePass123",
|
||||
Fingerprint: "device-fp-uuid", DeviceName: "Chrome на Windows", DeviceType: "WEB",
|
||||
})
|
||||
resp, err := http.Post("${API_BASE}/auth/login/password", "application/json", bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
var session map[string]any
|
||||
return session, json.NewDecoder(resp.Body).Decode(&session)
|
||||
}`
|
||||
},
|
||||
{
|
||||
id: 'rust',
|
||||
label: 'Rust',
|
||||
language: 'rust',
|
||||
code: `use reqwest::Client;
|
||||
use serde_json::json;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), reqwest::Error> {
|
||||
let client = Client::new();
|
||||
let response = client
|
||||
.post("${API_BASE}/auth/login/password")
|
||||
.json(&json!({
|
||||
"login": "user@example.com",
|
||||
"password": "SecurePass123",
|
||||
"fingerprint": "device-fp-uuid",
|
||||
"deviceName": "Chrome на Windows",
|
||||
"deviceType": "WEB"
|
||||
}))
|
||||
.send()
|
||||
.await?
|
||||
.error_for_status()?;
|
||||
|
||||
let session: serde_json::Value = response.json().await?;
|
||||
println!("{}", session["accessToken"]);
|
||||
Ok(())
|
||||
}`
|
||||
},
|
||||
{
|
||||
id: 'ruby',
|
||||
label: 'Ruby',
|
||||
language: 'ruby',
|
||||
code: `require 'net/http'
|
||||
require 'json'
|
||||
require 'uri'
|
||||
|
||||
uri = URI('${API_BASE}/auth/login/password')
|
||||
request = Net::HTTP::Post.new(uri)
|
||||
request['Content-Type'] = 'application/json'
|
||||
request.body = {
|
||||
login: 'user@example.com',
|
||||
password: 'SecurePass123',
|
||||
fingerprint: SecureRandom.uuid,
|
||||
deviceName: 'Chrome на Windows',
|
||||
deviceType: 'WEB'
|
||||
}.to_json
|
||||
|
||||
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
|
||||
http.request(request)
|
||||
end
|
||||
|
||||
raise response.body unless response.is_a?(Net::HTTPSuccess)
|
||||
session = JSON.parse(response.body)`
|
||||
},
|
||||
{
|
||||
id: 'java',
|
||||
label: 'Java',
|
||||
language: 'java',
|
||||
code: `import java.net.URI;
|
||||
import java.net.http.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
var body = """
|
||||
{
|
||||
"login": "user@example.com",
|
||||
"password": "SecurePass123",
|
||||
"fingerprint": "device-fp-uuid",
|
||||
"deviceName": "Chrome на Windows",
|
||||
"deviceType": "WEB"
|
||||
}
|
||||
""";
|
||||
|
||||
var request = HttpRequest.newBuilder()
|
||||
.uri(URI.create("${API_BASE}/auth/login/password"))
|
||||
.header("Content-Type", "application/json")
|
||||
.POST(HttpRequest.BodyPublishers.ofString(body, StandardCharsets.UTF_8))
|
||||
.build();
|
||||
|
||||
var response = HttpClient.newHttpClient()
|
||||
.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
|
||||
if (response.statusCode() >= 400) {
|
||||
throw new RuntimeException(response.body());
|
||||
}`
|
||||
},
|
||||
{
|
||||
id: 'csharp',
|
||||
label: 'C#',
|
||||
language: 'csharp',
|
||||
code: `using System.Net.Http.Json;
|
||||
|
||||
using var http = new HttpClient();
|
||||
|
||||
var payload = new {
|
||||
login = "user@example.com",
|
||||
password = "SecurePass123",
|
||||
fingerprint = Guid.NewGuid().ToString(),
|
||||
deviceName = "Chrome на Windows",
|
||||
deviceType = "WEB"
|
||||
};
|
||||
|
||||
var response = await http.PostAsJsonAsync("${API_BASE}/auth/login/password", payload);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var session = await response.Content.ReadFromJsonAsync<Dictionary<string, object>>();`
|
||||
},
|
||||
{
|
||||
id: 'kotlin',
|
||||
label: 'Kotlin',
|
||||
language: 'kotlin',
|
||||
code: `import io.ktor.client.*
|
||||
import io.ktor.client.request.*
|
||||
import io.ktor.client.statement.*
|
||||
import io.ktor.http.*
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class LoginRequest(
|
||||
val login: String,
|
||||
val password: String,
|
||||
val fingerprint: String,
|
||||
val deviceName: String,
|
||||
val deviceType: String
|
||||
)
|
||||
|
||||
suspend fun login(client: HttpClient): String {
|
||||
val response = client.post("${API_BASE}/auth/login/password") {
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(LoginRequest(
|
||||
login = "user@example.com",
|
||||
password = "SecurePass123",
|
||||
fingerprint = "device-fp-uuid",
|
||||
deviceName = "Chrome на Windows",
|
||||
deviceType = "WEB"
|
||||
))
|
||||
}
|
||||
if (!response.status.isSuccess()) error(response.bodyAsText())
|
||||
return response.bodyAsText()
|
||||
}`
|
||||
},
|
||||
{
|
||||
id: 'swift',
|
||||
label: 'Swift',
|
||||
language: 'swift',
|
||||
code: `import Foundation
|
||||
|
||||
struct LoginRequest: Encodable {
|
||||
let login: String
|
||||
let password: String
|
||||
let fingerprint: String
|
||||
let deviceName: String
|
||||
let deviceType: String
|
||||
}
|
||||
|
||||
var request = URLRequest(url: URL(string: "${API_BASE}/auth/login/password")!)
|
||||
request.httpMethod = "POST"
|
||||
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
||||
request.httpBody = try JSONEncoder().encode(LoginRequest(
|
||||
login: "user@example.com",
|
||||
password: "SecurePass123",
|
||||
fingerprint: UUID().uuidString,
|
||||
deviceName: "Chrome на Windows",
|
||||
deviceType: "WEB"
|
||||
))
|
||||
|
||||
let (data, response) = try await URLSession.shared.data(for: request)
|
||||
guard let http = response as? HTTPURLResponse, (200..<300).contains(http.statusCode) else {
|
||||
throw URLError(.badServerResponse)
|
||||
}`
|
||||
},
|
||||
{
|
||||
id: 'dart',
|
||||
label: 'Dart',
|
||||
language: 'dart',
|
||||
code: `import 'dart:convert';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
Future<Map<String, dynamic>> login() async {
|
||||
final response = await http.post(
|
||||
Uri.parse('${API_BASE}/auth/login/password'),
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: jsonEncode({
|
||||
'login': 'user@example.com',
|
||||
'password': 'SecurePass123',
|
||||
'fingerprint': 'device-fp-uuid',
|
||||
'deviceName': 'Chrome на Windows',
|
||||
'deviceType': 'WEB',
|
||||
}),
|
||||
);
|
||||
|
||||
if (response.statusCode >= 400) {
|
||||
throw Exception(response.body);
|
||||
}
|
||||
return jsonDecode(response.body) as Map<String, dynamic>;
|
||||
}`
|
||||
},
|
||||
{
|
||||
id: 'elixir',
|
||||
label: 'Elixir',
|
||||
language: 'elixir',
|
||||
code: `payload = Jason.encode!(%{
|
||||
login: "user@example.com",
|
||||
password: "SecurePass123",
|
||||
fingerprint: Ecto.UUID.generate(),
|
||||
deviceName: "Chrome на Windows",
|
||||
deviceType: "WEB"
|
||||
})
|
||||
|
||||
{:ok, %{status: status, body: body}} =
|
||||
Req.post("${API_BASE}/auth/login/password",
|
||||
headers: [{"content-type", "application/json"}],
|
||||
body: payload
|
||||
)
|
||||
|
||||
if status >= 400, do: raise(body)
|
||||
session = Jason.decode!(body)`
|
||||
},
|
||||
{
|
||||
id: 'powershell',
|
||||
label: 'PowerShell',
|
||||
language: 'powershell',
|
||||
code: [
|
||||
'$body = @{',
|
||||
" login = 'user@example.com'",
|
||||
" password = 'SecurePass123'",
|
||||
' fingerprint = [guid]::NewGuid().ToString()',
|
||||
" deviceName = 'Chrome на Windows'",
|
||||
" deviceType = 'WEB'",
|
||||
'} | ConvertTo-Json',
|
||||
'',
|
||||
'$response = Invoke-RestMethod `',
|
||||
' -Method Post `',
|
||||
` -Uri '${API_BASE}/auth/login/password' \``,
|
||||
" -ContentType 'application/json' `",
|
||||
' -Body $body',
|
||||
'',
|
||||
'$response.accessToken'
|
||||
].join('\n')
|
||||
}
|
||||
];
|
||||
|
||||
export const authLdapExamples: CodeExample[] = [
|
||||
{
|
||||
id: 'curl-ldap',
|
||||
label: 'cURL',
|
||||
language: 'bash',
|
||||
code: `curl -X POST ${API_BASE}/auth/ldap/login \\
|
||||
-H "Content-Type: application/json" \\
|
||||
-d '{
|
||||
"username": "ivan.petrov",
|
||||
"password": "ldap-password",
|
||||
"fingerprint": "device-fp-uuid",
|
||||
"deviceName": "Chrome",
|
||||
"deviceType": "WEB"
|
||||
}'`
|
||||
},
|
||||
{
|
||||
id: 'javascript-ldap',
|
||||
label: 'JavaScript',
|
||||
language: 'javascript',
|
||||
code: `const response = await fetch('${API_BASE}/auth/ldap/login', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
username: 'ivan.petrov',
|
||||
password: 'ldap-password',
|
||||
fingerprint: crypto.randomUUID(),
|
||||
deviceName: 'Chrome',
|
||||
deviceType: 'WEB'
|
||||
})
|
||||
});
|
||||
const session = await response.json();`
|
||||
},
|
||||
{
|
||||
id: 'python-ldap',
|
||||
label: 'Python',
|
||||
language: 'python',
|
||||
code: `import requests
|
||||
|
||||
session = requests.post('${API_BASE}/auth/ldap/login', json={
|
||||
'username': 'ivan.petrov',
|
||||
'password': 'ldap-password',
|
||||
'fingerprint': 'device-fp-uuid',
|
||||
'deviceName': 'Chrome',
|
||||
'deviceType': 'WEB',
|
||||
}, timeout=15).json()`
|
||||
},
|
||||
{
|
||||
id: 'php-ldap',
|
||||
label: 'PHP',
|
||||
language: 'php',
|
||||
code: `<?php
|
||||
$ch = curl_init('${API_BASE}/auth/ldap/login');
|
||||
curl_setopt_array($ch, [
|
||||
CURLOPT_POST => true,
|
||||
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
|
||||
CURLOPT_POSTFIELDS => json_encode([
|
||||
'username' => 'ivan.petrov',
|
||||
'password' => 'ldap-password',
|
||||
'fingerprint' => bin2hex(random_bytes(16)),
|
||||
'deviceName' => 'Chrome',
|
||||
'deviceType' => 'WEB',
|
||||
]),
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
]);
|
||||
$session = json_decode(curl_exec($ch), true);
|
||||
curl_close($ch);`
|
||||
},
|
||||
{
|
||||
id: 'go-ldap',
|
||||
label: 'Go',
|
||||
language: 'go',
|
||||
code: `body, _ := json.Marshal(map[string]string{
|
||||
"username": "ivan.petrov", "password": "ldap-password",
|
||||
"fingerprint": "device-fp-uuid", "deviceName": "Chrome", "deviceType": "WEB",
|
||||
})
|
||||
resp, _ := http.Post("${API_BASE}/auth/ldap/login", "application/json", bytes.NewReader(body))
|
||||
defer resp.Body.Close()
|
||||
json.NewDecoder(resp.Body).Decode(&session)`
|
||||
},
|
||||
{
|
||||
id: 'rust-ldap',
|
||||
label: 'Rust',
|
||||
language: 'rust',
|
||||
code: `let session = reqwest::Client::new()
|
||||
.post("${API_BASE}/auth/ldap/login")
|
||||
.json(&serde_json::json!({
|
||||
"username": "ivan.petrov",
|
||||
"password": "ldap-password",
|
||||
"fingerprint": "device-fp-uuid",
|
||||
"deviceName": "Chrome",
|
||||
"deviceType": "WEB"
|
||||
}))
|
||||
.send().await?
|
||||
.json::<serde_json::Value>().await?;`
|
||||
}
|
||||
];
|
||||
Reference in New Issue
Block a user