import { createOIDCProvider } from '@repo/oidc-provider';
import { RedisStorageAdapter } from '@repo/oidc-provider';
import type { OIDCProviderConfig } from '@repo/oidc-provider';
import redis from '../redis';
import { clients } from './clients';
import { users } from './users';
import { validatePassword } from './auth';

// OIDC Provider 配置
const oidcConfig: OIDCProviderConfig = {
    issuer: 'http://localhost:3000/oidc',
    storage: new RedisStorageAdapter(redis),
    findClient: clients.findClient,
    findUser: users.findUser,
    authConfig: {
        passwordValidator: validatePassword,
        sessionTTL: 24 * 60 * 60, // 24小时
        pageConfig: {
            title: 'OIDC Provider 登录',
            brandName: 'Nice OIDC Provider',
        },
    },
    tokenTTL: {
        accessToken: 3600, // 1小时
        refreshToken: 30 * 24 * 3600, // 30天
        authorizationCode: 600, // 10分钟
        idToken: 3600, // 1小时
    },
    responseTypes: ['code'],
    grantTypes: ['authorization_code', 'refresh_token'],
    scopes: ['openid', 'profile', 'email', 'phone', 'address'],
    claims: [
        'sub', 'name', 'given_name', 'family_name', 'middle_name', 'nickname',
        'preferred_username', 'profile', 'picture', 'website', 'email',
        'email_verified', 'gender', 'birthdate', 'zoneinfo', 'locale',
        'phone_number', 'phone_number_verified', 'address', 'updated_at'
    ],
    enablePKCE: true,
    requirePKCE: false,
    rotateRefreshTokens: true,
};

// 创建OIDC Provider应用
export const oidcApp = createOIDCProvider(oidcConfig); 