mirror of
https://github.com/unraid/api.git
synced 2026-01-04 23:50:37 -06:00
rm docker-auth.service
This commit is contained in:
@@ -1,73 +0,0 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { readFile } from 'fs/promises';
|
||||
import { join } from 'path';
|
||||
|
||||
import type { Got } from 'got';
|
||||
import { ExtendOptions, got as gotClient } from 'got';
|
||||
|
||||
export type BasicDockerCreds = {
|
||||
username: string;
|
||||
password: string;
|
||||
};
|
||||
|
||||
export type DockerWwwAuthParts = {
|
||||
realm: string;
|
||||
service: string;
|
||||
scope: string;
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
export class DockerAuthService {
|
||||
private readonly logger = new Logger(DockerAuthService.name);
|
||||
constructor() {}
|
||||
|
||||
async readDockerAuth(configPath?: string) {
|
||||
try {
|
||||
configPath ??= join(process.env.HOME || '/root', '.docker/config.json');
|
||||
const cfg = JSON.parse(await readFile(configPath, 'utf8'));
|
||||
return cfg.auths || {};
|
||||
} catch (error) {
|
||||
this.logger.debug(error, `Failed to read Docker auth from '${configPath}'`);
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
decodeAuth(auth: string): BasicDockerCreds {
|
||||
try {
|
||||
const [username, password] = Buffer.from(auth, 'base64').toString('utf8').split(':');
|
||||
return { username, password };
|
||||
} catch {
|
||||
return { username: '', password: '' };
|
||||
}
|
||||
}
|
||||
|
||||
parseWWWAuth(wwwAuth: string): Partial<DockerWwwAuthParts> {
|
||||
// www-authenticate: Bearer realm="...",service="...",scope="repository:repo/name:pull"
|
||||
const parts: Partial<DockerWwwAuthParts> = {};
|
||||
const rawParts = wwwAuth.replace(/^Bearer\s+/i, '').split(',') || [];
|
||||
rawParts.forEach((pair) => {
|
||||
const [k, v] = pair.split('=');
|
||||
parts[k.trim()] = v?.replace(/^"|"$/g, '');
|
||||
});
|
||||
return parts;
|
||||
}
|
||||
|
||||
async getBearerToken(
|
||||
wwwAuth: string,
|
||||
basicCreds: BasicDockerCreds,
|
||||
got: Got<ExtendOptions> = gotClient
|
||||
) {
|
||||
const parts = this.parseWWWAuth(wwwAuth);
|
||||
if (!parts.realm || !parts.service || !parts.scope) return null;
|
||||
const { token } = await got
|
||||
.get(parts.realm, {
|
||||
searchParams: { service: parts.service, scope: parts.scope },
|
||||
username: basicCreds.username,
|
||||
password: basicCreds.password,
|
||||
timeout: { request: 15_000 },
|
||||
responseType: 'json',
|
||||
})
|
||||
.json<{ token?: string }>();
|
||||
return token;
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,6 @@ import { readFile } from 'fs/promises';
|
||||
import { ExtendOptions, Got, got as gotClient, OptionsOfTextResponseBody } from 'got';
|
||||
|
||||
import { docker } from '@app/core/utils/index.js';
|
||||
import { DockerAuthService } from '@app/unraid-api/graph/resolvers/docker/docker-auth.service.js';
|
||||
|
||||
/** Accept header for Docker API manifest listing */
|
||||
const ACCEPT_MANIFEST =
|
||||
@@ -21,8 +20,7 @@ export type CachedStatusEntry = {
|
||||
|
||||
@Injectable()
|
||||
export class DockerManifestService {
|
||||
private readonly logger = new Logger(DockerManifestService.name);
|
||||
constructor(private readonly dockerAuthService: DockerAuthService) {}
|
||||
constructor() {}
|
||||
|
||||
async readCachedUpdateStatus(cacheFile = '/var/lib/docker/unraid-update-status.json') {
|
||||
const cache = await readFile(cacheFile, 'utf8');
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
|
||||
import { ContainerStatusJob } from '@app/unraid-api/graph/resolvers/docker/container-status.job.js';
|
||||
import { DockerAuthService } from '@app/unraid-api/graph/resolvers/docker/docker-auth.service.js';
|
||||
import { DockerConfigService } from '@app/unraid-api/graph/resolvers/docker/docker-config.service.js';
|
||||
import { DockerContainerResolver } from '@app/unraid-api/graph/resolvers/docker/docker-container.resolver.js';
|
||||
import { DockerManifestService } from '@app/unraid-api/graph/resolvers/docker/docker-manifest.service.js';
|
||||
@@ -17,7 +16,6 @@ import { DockerService } from '@app/unraid-api/graph/resolvers/docker/docker.ser
|
||||
DockerService,
|
||||
DockerConfigService,
|
||||
DockerOrganizerService,
|
||||
DockerAuthService,
|
||||
DockerManifestService,
|
||||
DockerPhpService,
|
||||
// DockerEventService,
|
||||
|
||||
Reference in New Issue
Block a user