osu-api-extended
Version:
Advanced osu! api wrapper for v1 and v2, with extra stuff
173 lines • 6.72 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.authorize = exports.build_url = exports.authorize_cli = exports.login = exports.login_lazer = exports.re_login = exports.set_v2 = exports.set_v1 = exports.cache_v2 = exports.cache_v1 = void 0;
const child_process_1 = require("child_process");
const readline_1 = __importDefault(require("readline"));
const request_1 = require("../utility/request");
const credentials = {
type: 0,
username: '',
password: '',
clientId: 0,
clientSecret: '',
redirect_uri: '',
scope: ['public'],
};
exports.cache_v1 = '';
exports.cache_v2 = '';
const set_v1 = (v) => exports.cache_v1 = v;
exports.set_v1 = set_v1;
const set_v2 = (v) => exports.cache_v2 = v;
exports.set_v2 = set_v2;
const isInitial = () => exports.cache_v2 != '';
const save_credentials = (type, obj) => {
if (type == 1) {
credentials.type = 1;
credentials.username = obj.username;
credentials.password = obj.password;
}
;
if (type == 2) {
credentials.type = 2;
credentials.clientId = obj.clientId;
credentials.clientSecret = obj.clientSecret;
credentials.scope = obj.scope;
}
;
if (type == 3) {
credentials.type = 3;
credentials.clientId = obj.clientId;
credentials.clientSecret = obj.clientSecret;
credentials.redirect_uri = obj.redirect_uri;
credentials.scope = obj.scope;
}
;
};
const re_login = async () => {
if (credentials.type == 1)
await (0, exports.login_lazer)(credentials.username, credentials.password);
if (credentials.type == 2)
await (0, exports.login)(credentials.clientId, credentials.clientSecret, credentials.scope);
if (credentials.type == 3)
await (0, exports.authorize_cli)(credentials.clientId, credentials.clientSecret, credentials.redirect_uri, credentials.scope);
return true;
};
exports.re_login = re_login;
const login_lazer = async (username, password) => {
if (!isInitial())
save_credentials(1, { username, password });
const { access_token, expires_in } = await (0, request_1.request)('https://osu.ppy.sh/oauth/token', {
method: 'POST',
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
'x-api-version': '20240130',
},
data: JSON.stringify({
username,
password,
grant_type: "password",
client_id: 5,
client_secret: 'FGc9GAtyHzeQDshWP5Ah7dega8hJACAJpQtw6OXk',
scope: "*"
})
});
exports.cache_v2 = access_token;
return { access_token, expires_in };
};
exports.login_lazer = login_lazer;
const login = async (clientId, clientSecret, scope) => {
if (!Array.isArray(scope) || !scope)
throw new Error('Scope must be an Array');
if (!isInitial())
save_credentials(2, { clientId, clientSecret, scope });
const { access_token, expires_in } = await (0, request_1.request)('https://osu.ppy.sh/oauth/token', {
method: 'POST',
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
'x-api-version': '20240130',
},
data: JSON.stringify({
grant_type: 'client_credentials',
client_id: clientId,
client_secret: clientSecret,
scope: scope.join(' '),
code: 'code',
})
});
exports.cache_v2 = access_token;
return { access_token, expires_in };
};
exports.login = login;
const authorize_cli = async (clientId, clientSecret, redirectUri, scope, state) => {
if (!Array.isArray(scope) || !scope)
throw new Error('Scope must be an Array');
if (!isInitial())
save_credentials(3, { clientId, clientSecret, redirect_uri: redirectUri, scope });
const cl = readline_1.default.createInterface(process.stdin, process.stdout);
const question = (q) => new Promise((res, rej) => cl.question(q + ': ', (answer) => res(answer)));
const url = (0, exports.build_url)(clientId, redirectUri, scope, state);
// await open(url);
const start = (process.platform == 'darwin' ? 'open' : process.platform == 'win32' ? 'start' : 'xdg-open');
(0, child_process_1.execSync)(start + ' ' + url.replaceAll('&', '^&'));
const code = await question('Paste code here');
const { access_token, expires_in } = await (0, request_1.request)('https://osu.ppy.sh/oauth/token', {
method: 'POST',
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
'x-api-version': '20240130',
},
data: JSON.stringify({
grant_type: 'authorization_code',
client_id: clientId,
client_secret: clientSecret,
redirect_uri: redirectUri,
code,
})
});
exports.cache_v2 = access_token;
return { access_token, expires_in };
};
exports.authorize_cli = authorize_cli;
const build_url = (clientId, redirectUri, scope, state) => {
if (!Array.isArray(scope) || !scope)
throw new Error('Scope must be an Array');
const url = new URL('https://osu.ppy.sh/oauth/authorize');
const params = {
client_id: clientId,
redirect_uri: redirectUri,
response_type: 'code',
scope: scope.join(' '),
state: state,
};
Object.keys(params)
.forEach(key => url.searchParams.append(key, params[key]));
return url.href;
};
exports.build_url = build_url;
const authorize = async (code, gamemode, clientId, clientSecret, redirectUri) => {
const { access_token } = await (0, request_1.request)('https://osu.ppy.sh/oauth/token', {
method: 'POST',
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
'x-api-version': '20240130',
},
data: JSON.stringify({
grant_type: 'authorization_code',
client_id: clientId,
client_secret: clientSecret,
redirect_uri: redirectUri,
code,
})
});
const user = await (0, request_1.request)(`https://osu.ppy.sh/api/v2/me/${gamemode}`, { params: { v2: access_token } });
return user;
};
exports.authorize = authorize;
//# sourceMappingURL=auth.js.map
;