소스 검색

finish oauth2 verify

Markus-Rost 4 년 전
부모
커밋
6f2707ef81
4개의 변경된 파일50개의 추가작업 그리고 35개의 파일을 삭제
  1. 14 8
      cmds/verify.js
  2. 6 7
      dashboard/oauth.js
  3. 2 4
      functions/verify.js
  4. 28 16
      interactions/verify.js

+ 14 - 8
cmds/verify.js

@@ -33,17 +33,20 @@ function cmd_verify(lang, msg, args, line, wiki) {
 			if ( wiki.isWikimedia() ) oauth = 'wikimedia';
 			if ( wiki.isMiraheze() ) oauth = 'miraheze';
 			if ( oauth && process.env[`oauth-${oauth}`] && process.env[`oauth-${oauth}-secret`] ) {
-				let state = `${oauth}-${global.shardId}` + Date.now().toString(16) + randomBytes(16).toString('hex');
+				let state = `${oauth} ${wiki.hostname} ${global.shardId}` + Date.now().toString(16) + randomBytes(16).toString('hex');
 				while ( oauthVerify.has(state) ) {
-					state = `${oauth}-${global.shardId}` + Date.now().toString(16) + randomBytes(16).toString('hex');
+					state = `${oauth} ${wiki.hostname} ${global.shardId}` + Date.now().toString(16) + randomBytes(16).toString('hex');
 				}
 				oauthVerify.set(state, {
-					state, wiki: oauth,
+					state, wiki: wiki.hostname,
 					channel: msg.channel,
 					user: msg.author.id
 				});
 				msg.client.shard.send({id: 'verifyUser', state});
-				let oauthURL = `https://meta.${oauth}.org/w/rest.php/oauth2/authorize?response_type=code&redirect_uri=${encodeURIComponent('https://settings.wikibot.de/oauth/mw')}&client_id=${process.env['oauth-' + oauth]}&state=${state}`;
+				let oauthURL = wiki + 'rest.php/oauth2/authorize?' + new URLSearchParams({
+					response_type: 'code', redirect_uri: new URL('/oauth/mw', process.env.dashboard).href,
+					client_id: process.env[`oauth-${oauth}`], state
+				}).toString();
 				return msg.member.send( lang.get('verify.oauth_message_dm', escapeFormatting(msg.guild.name)) + '\n<' + oauthURL + '>', {
 					components: [
 						{
@@ -87,17 +90,20 @@ function cmd_verify(lang, msg, args, line, wiki) {
 		msg.reactEmoji('⏳').then( reaction => {
 			verify(lang, msg.channel, msg.member, username, wiki, rows).then( result => {
 				if ( result.oauth ) {
-					let state = `${result.oauth}-${global.shardId}` + Date.now().toString(16) + randomBytes(16).toString('hex');
+					let state = `${result.oauth} ${wiki.hostname} ${global.shardId}` + Date.now().toString(16) + randomBytes(16).toString('hex');
 					while ( oauthVerify.has(state) ) {
-						state = `${result.oauth}-${global.shardId}` + Date.now().toString(16) + randomBytes(16).toString('hex');
+						state = `${result.oauth} ${wiki.hostname} ${global.shardId}` + Date.now().toString(16) + randomBytes(16).toString('hex');
 					}
 					oauthVerify.set(state, {
-						state, wiki: result.oauth,
+						state, wiki: wiki.hostname,
 						channel: msg.channel,
 						user: msg.author.id
 					});
 					msg.client.shard.send({id: 'verifyUser', state});
-					let oauthURL = `https://meta.${result.oauth}.org/w/rest.php/oauth2/authorize?response_type=code&redirect_uri=${encodeURIComponent('https://settings.wikibot.de/oauth/mw')}&client_id=${process.env['oauth-' + result.oauth]}&state=${state}`;
+					let oauthURL = wiki + 'rest.php/oauth2/authorize?' + new URLSearchParams({
+						response_type: 'code', redirect_uri: new URL('/oauth/mw', process.env.dashboard).href,
+						client_id: process.env[`oauth-${result.oauth}`], state
+					}).toString();
 					msg.member.send( lang.get('verify.oauth_message_dm', escapeFormatting(msg.guild.name)) + '\n<' + oauthURL + '>', {
 						components: [
 							{

+ 6 - 7
dashboard/oauth.js

@@ -325,18 +325,17 @@ function mediawiki_oauth(res, searchParams) {
 		return res.end();
 	}
 	var state = searchParams.get('state');
-	var site = state.split('-')[0];
-	got.post( 'https://meta.' + site + '.org/w/rest.php/oauth2/access_token', {
+	var site = state.split(' ');
+	got.post( 'https://' + site[1] + '/w/rest.php/oauth2/access_token', {
 		form: {
 			grant_type: 'authorization_code',
 			code: searchParams.get('code'),
-			redirect_uri: new URL('https://settings.wikibot.de/oauth/mw', process.env.dashboard).href,
-			client_id: process.env[`oauth-${site}`],
-			client_secret: process.env[`oauth-${site}-secret`]
+			redirect_uri: new URL('/oauth/mw', process.env.dashboard).href,
+			client_id: process.env[`oauth-${site[0]}`],
+			client_secret: process.env[`oauth-${site[0]}-secret`]
 		}
 	} ).then( response => {
 		var body = response.body;
-		console.log(response.statusCode,body)
 		if ( response.statusCode !== 200 || !body?.access_token ) {
 			console.log( '- Dashboard: ' + response.statusCode + ': Error while getting the mediawiki token: ' + ( body?.message || body?.error ) );
 			res.writeHead(302, {Location: '/login?action=failed'});
@@ -347,7 +346,7 @@ function mediawiki_oauth(res, searchParams) {
 			access_token: body.access_token
 		} ).then( () => {
 			oauthVerify.delete(state);
-			res.writeHead(302, {Location: '/login?action=success'});
+			res.writeHead(302, {Location: 'https://' + site[1] + '/wiki/Special:MyPage'});
 			return res.end();
 		}, error => {
 			console.log( '- Dashboard: Error while sending the mediawiki token: ' + error );

+ 2 - 4
functions/verify.js

@@ -493,7 +493,7 @@ global.verifyOauthUser = function(state, access_token, settings) {
 			} );
 		} ),
 		channel.guild.members.fetch(settings.user),
-		( !username ? got.get( 'https://meta.' + settings.wiki + '.org/w/rest.php/oauth2/resource/profile', {
+		( !username ? got.get( 'https://' + settings.wiki + '/w/rest.php/oauth2/resource/profile', {
 			Authorization: `Bearer ${access_token}`
 		} ).then( response => {
 			var body = response.body;
@@ -507,7 +507,7 @@ global.verifyOauthUser = function(state, access_token, settings) {
 			console.log( '- Error while getting the mediawiki profile: ' + error );
 		} ) : null )
 	]).then( ([{rows, wiki, lang}, member]) => {
-		if ( !username ) return settings.edit?.();
+		if ( !username || ( settings.wiki && settings.wiki !== wiki.hostname ) ) return settings.edit?.();
 		got.get( wiki + 'api.php?action=query&meta=siteinfo|globaluserinfo&siprop=general&guiprop=groups&guiuser=' + encodeURIComponent( username ) + '&list=users&usprop=blockinfo|groups|editcount|registration|gender&ususers=' + encodeURIComponent( username ) + '&format=json' ).then( response => {
 			var body = response.body;
 			if ( body && body.warnings ) log_warn(body.warnings);
@@ -521,8 +521,6 @@ global.verifyOauthUser = function(state, access_token, settings) {
 				return settings.edit?.();
 			}
 			wiki.updateWiki(body.query.general);
-			if ( settings.wiki === 'wikimedia' && !wiki.isWikimedia() ) return settings.edit?.();
-			if ( settings.wiki === 'miraheze' && !wiki.isMiraheze() ) return settings.edit?.();
 			logging(wiki, channel.guild.id, 'verification');
 			var queryuser = body.query.users[0];
 			if ( body.query.users.length !== 1 || queryuser.missing !== undefined || queryuser.invalid !== undefined ) return settings.edit?.();

+ 28 - 16
interactions/verify.js

@@ -58,16 +58,19 @@ function slash_verify(interaction, lang, wiki, channel) {
 			if ( wiki.isWikimedia() ) oauth = 'wikimedia';
 			if ( wiki.isMiraheze() ) oauth = 'miraheze';
 			if ( oauth && process.env[`oauth-${oauth}`] && process.env[`oauth-${oauth}-secret`] ) {
-				let state = `${oauth}-${global.shardId}` + Date.now().toString(16) + randomBytes(16).toString('hex');
+				let state = `${oauth} ${wiki.hostname} ${global.shardId}` + Date.now().toString(16) + randomBytes(16).toString('hex');
 				while ( oauthVerify.has(state) ) {
-					state = `${oauth}-${global.shardId}` + Date.now().toString(16) + randomBytes(16).toString('hex');
+					state = `${oauth} ${wiki.hostname} ${global.shardId}` + Date.now().toString(16) + randomBytes(16).toString('hex');
 				}
 				oauthVerify.set(state, {
-					state, wiki: oauth, channel,
+					state, wiki: wiki.hostname, channel,
 					user: interaction.user.id
 				});
 				interaction.client.shard.send({id: 'verifyUser', state});
-				let oauthURL = `https://meta.${oauth}.org/w/rest.php/oauth2/authorize?response_type=code&redirect_uri=${encodeURIComponent('https://settings.wikibot.de/oauth/mw')}&client_id=${process.env['oauth-' + oauth]}&state=${state}`;
+				let oauthURL = wiki + 'rest.php/oauth2/authorize?' + new URLSearchParams({
+					response_type: 'code', redirect_uri: new URL('/oauth/mw', process.env.dashboard).href,
+					client_id: process.env[`oauth-${oauth}`], state
+				}).toString();
 				return interaction.client.api.interactions(interaction.id, interaction.token).callback.post( {
 					data: {
 						type: 4,
@@ -136,16 +139,19 @@ function slash_verify(interaction, lang, wiki, channel) {
 			return channel.guild.members.fetch(interaction.user.id).then( member => {
 				return verify(lang, channel, member, username, wiki, rows).then( result => {
 					if ( result.oauth ) {
-						let state = `${result.oauth}-${global.shardId}` + Date.now().toString(16) + randomBytes(16).toString('hex');
+						let state = `${result.oauth} ${wiki.hostname} ${global.shardId}` + Date.now().toString(16) + randomBytes(16).toString('hex');
 						while ( oauthVerify.has(state) ) {
-							state = `${result.oauth}-${global.shardId}` + Date.now().toString(16) + randomBytes(16).toString('hex');
+							state = `${result.oauth} ${wiki.hostname} ${global.shardId}` + Date.now().toString(16) + randomBytes(16).toString('hex');
 						}
 						oauthVerify.set(state, {
-							state, wiki: result.oauth, channel,
+							state, wiki: wiki.hostname, channel,
 							user: interaction.user.id
 						});
 						interaction.client.shard.send({id: 'verifyUser', state});
-						let oauthURL = `https://meta.${result.oauth}.org/w/rest.php/oauth2/authorize?response_type=code&redirect_uri=${encodeURIComponent('https://settings.wikibot.de/oauth/mw')}&client_id=${process.env['oauth-' + result.oauth]}&state=${state}`;
+						let oauthURL = wiki + 'rest.php/oauth2/authorize?' + new URLSearchParams({
+							response_type: 'code', redirect_uri: new URL('/oauth/mw', process.env.dashboard).href,
+							client_id: process.env[`oauth-${result.oauth}`], state
+						}).toString();
 						return interaction.client.api.webhooks(interaction.application_id, interaction.token).messages('@original').delete().then( () => {
 							return interaction.client.api.webhooks(interaction.application_id, interaction.token).post( {
 								data: {
@@ -323,16 +329,19 @@ function slash_verify(interaction, lang, wiki, channel) {
 			if ( wiki.isWikimedia() ) oauth = 'wikimedia';
 			if ( wiki.isMiraheze() ) oauth = 'miraheze';
 			if ( oauth && process.env[`oauth-${oauth}`] && process.env[`oauth-${oauth}-secret`] ) {
-				let state = `${oauth}-${global.shardId}` + Date.now().toString(16) + randomBytes(16).toString('hex');
+				let state = `${oauth} ${wiki.hostname} ${global.shardId}` + Date.now().toString(16) + randomBytes(16).toString('hex');
 				while ( oauthVerify.has(state) ) {
-					state = `${oauth}-${global.shardId}` + Date.now().toString(16) + randomBytes(16).toString('hex');
+					state = `${oauth} ${wiki.hostname} ${global.shardId}` + Date.now().toString(16) + randomBytes(16).toString('hex');
 				}
 				oauthVerify.set(state, {
-					state, wiki: oauth, channel,
+					state, wiki: wiki.hostname, channel,
 					user: interaction.user.id
 				});
 				interaction.client.shard.send({id: 'verifyUser', state});
-				let oauthURL = `https://meta.${oauth}.org/w/rest.php/oauth2/authorize?response_type=code&redirect_uri=${encodeURIComponent('https://settings.wikibot.de/oauth/mw')}&client_id=${process.env['oauth-' + oauth]}&state=${state}`;
+				let oauthURL = wiki + 'rest.php/oauth2/authorize?' + new URLSearchParams({
+					response_type: 'code', redirect_uri: new URL('/oauth/mw', process.env.dashboard).href,
+					client_id: process.env[`oauth-${oauth}`], state
+				}).toString();
 				interaction.message.components = [];
 				interaction.client.api.interactions(interaction.id, interaction.token).callback.post( {
 					data: {
@@ -376,16 +385,19 @@ function slash_verify(interaction, lang, wiki, channel) {
 				console.log( interaction.guild_id + ': Button: ' + interaction.data.custom_id + ' ' + username );
 				return verify(lang, channel, member, username, wiki, rows).then( result => {
 					if ( result.oauth ) {
-						let state = `${result.oauth}-${global.shardId}` + Date.now().toString(16) + randomBytes(16).toString('hex');
+						let state = `${result.oauth} ${wiki.hostname} ${global.shardId}` + Date.now().toString(16) + randomBytes(16).toString('hex');
 						while ( oauthVerify.has(state) ) {
-							state = `${result.oauth}-${global.shardId}` + Date.now().toString(16) + randomBytes(16).toString('hex');
+							state = `${result.oauth} ${wiki.hostname} ${global.shardId}` + Date.now().toString(16) + randomBytes(16).toString('hex');
 						}
 						oauthVerify.set(state, {
-							state, wiki: result.oauth, channel,
+							state, wiki: wiki.hostname, channel,
 							user: interaction.user.id
 						});
 						interaction.client.shard.send({id: 'verifyUser', state});
-						let oauthURL = `https://meta.${result.oauth}.org/w/rest.php/oauth2/authorize?response_type=code&redirect_uri=${encodeURIComponent('https://settings.wikibot.de/oauth/mw')}&client_id=${process.env['oauth-' + result.oauth]}&state=${state}`;
+						let oauthURL = wiki + 'rest.php/oauth2/authorize?' + new URLSearchParams({
+							response_type: 'code', redirect_uri: new URL('/oauth/mw', process.env.dashboard).href,
+							client_id: process.env[`oauth-${result.oauth}`], state
+						}).toString();
 						interaction.message.components = [];
 						interaction.client.api.interactions(interaction.id, interaction.token).callback.post( {
 							data: {