Add token flag

Allows for login using refresh token in crunchy. Adds #597
This commit is contained in:
AnimeDL 2024-03-18 17:44:49 -07:00
parent 6280d13d36
commit b497fb40df
3 changed files with 39 additions and 0 deletions

View file

@ -101,6 +101,9 @@ export default class Crunchy implements ServiceClass {
password: argv.password ?? await shlp.question('[Q] PASSWORD ')
});
}
else if (argv.token) {
await this.loginWithToken(argv.token);
}
else if(argv.cmsindex){
await this.refreshToken();
await this.getCmsData();
@ -289,6 +292,29 @@ export default class Crunchy implements ServiceClass {
});
}
public async loginWithToken(refreshToken: string) {
const authData = new URLSearchParams({
'refresh_token': refreshToken,
'grant_type': 'refresh_token',
'scope': 'offline_access'
}).toString();
const authReqOpts: reqModule.Params = {
method: 'POST',
headers: api.beta_authHeaderMob,
body: authData
};
const authReq = await this.req.getData(api.beta_auth, authReqOpts);
if(!authReq.ok || !authReq.res){
console.error('Token Authentication failed!');
return;
}
this.token = JSON.parse(authReq.res.body);
this.token.expires = new Date(Date.now() + this.token.expires_in);
yamlCfg.saveCRToken(this.token);
await this.getProfile(false);
await this.getCMStoken(true);
}
public async refreshToken(ifNeeded = false, silent = false) {
if(!this.token.access_token && !this.token.refresh_token || this.token.access_token && !this.token.refresh_token){
await this.doAnonymousAuth();

View file

@ -17,6 +17,7 @@ let argvC: {
forceMuxer: AvailableMuxer|undefined;
username: string|undefined,
password: string|undefined,
token: string|undefined,
silentAuth: boolean,
skipSubMux: boolean,
downloadArchive: boolean,

View file

@ -673,6 +673,18 @@ const args: TAppArg<boolean|number|string|unknown[]>[] = [
default: false
}
},
{
name: 'token',
describe: 'Allows you to login with your token (Example on crunchy is Refresh Token/etp-rt cookie)',
docDescribe: true,
group: 'auth',
service: ['crunchy'],
type: 'string',
usage: '${token}',
default: {
default: undefined
}
},
{
name: 'forceMuxer',
describe: 'Force the program to use said muxer or don\'t mux if the given muxer is not present',