Api server functionality is added

Cape support is added
This commit is contained in:
András Rutkai committed 2015-10-23 16:51:08 +02:00
1 parent 7f98987585
commit 4b628a9c6f
23 files changed
+533 -44

No files matched your search

+5 -3
View File
@@ -1,6 +1,8 @@
/images/*
!/images/.gitkeep
!/images/steve.png
/images/*.*
/images/skin/*
/images/cape/*
!/images/*/.gitkeep
!/images/skin/steve.png
/static/*
!/static/.gitkeep
node_modules
+8 -1
View File
@@ -37,13 +37,19 @@ app.all('/invalidate', actions.invalidate);
// Sessionserver
app.all('/session/minecraft/join', actions.sessionJoin);
app.all('/session/minecraft/hasJoined', actions.sessionHasJoined);
app.all('/session/minecraft/profile/:uuid', actions.sessionUUIDToProfile);
// APIserver
app.all('/users/profiles/minecraft/:name', actions.apiNameToUUID);
app.all('/user/profiles/:uuid/names', actions.apiUUIDToName);
app.all('/profiles/minecraft', actions.apiPlayernamesToUUID);
// Other
app.all('*', function (request, response) {
console.log('Unknown request: ');
console.log(request.headers.host, request.originalUrl);
console.log(request.body);
response.json({
response.status(404).json({
error: "Not Found",
errorMessage: "The server has not found anything matching the request URI"
});
@@ -52,6 +58,7 @@ app.all('*', function (request, response) {
startServer('sessionserver');
startServer('authserver');
startServer('apiserver');
function startServer(server) {
File renamed without changes.
+9
View File
@@ -22,6 +22,15 @@
"commonName": "sessionserver.mojang.com"
}
},
"apiserver": {
"httpPort": 9020,
"httpsPort": 9021,
"certification": {
"days": 365,
"selfSigned": true,
"commonName": "apiserver.mojang.com"
}
},
"drivers": ["config"],
"users": [
]
+7 -2
View File
@@ -11,20 +11,25 @@
"httpPort": 6677,
"httpsPort": 6678
},
"apiserver": {
"httpPort": 6688,
"httpsPort": 6689
},
"users": [
{
"id": "650bed2c-9ef5-4b5f-b02c-61fa493c68b5",
"username": "test",
"password": "098f6bcd4621d373cade4e832627b4f6", // test using MD5
"playerName": "testPlayer",
"skinUrl": "https://minecraft.net/images/steve.png"
"skinUrl": "steve.png"
},
{
"id": "1294fda6-159c-4218-be4c-89b660d9cf32",
"username": "testOld",
"password": "098f6bcd4621d373cade4e832627b4f6",
"playerName": "testPlayerOld",
"skinUrl": "https://minecraft.net/images/steve.png",
"skinUrl": "steve.png",
"capeUrl": "mycape.png",
"clientToken": "test-old-client-token",
"accessToken": "d41d8cd98f00b204e9800998ecf8427e",
"lastLogin": "2015-04-23T18:25:43.511Z"
+1
View File
@@ -2,6 +2,7 @@
openssl genrsa -out cert/authserver/server.key 4096
openssl genrsa -out cert/sessionserver/server.key 4096
openssl genrsa -out cert/apiserver/server.key 4096
echo -e " The Certificates and Keys have been generated!"
cd ..
View File
Whitespace-only changes.
View File
Whitespace-only changes.
File renamed without changes.

Before

Width:  |  Height:  |  Size: 1.3 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

+156 -24
View File
@@ -208,35 +208,14 @@ function sessionJoin(request, response) {
exports.sessionHasJoined = sessionHasJoined;
function sessionHasJoined(request, response) {
var jsonReponse, user, profileId, value;
var jsonReponse, user;
logger.log("Session has joined request");
if (request.query.username && request.query.serverId && (user = userManager.sessionHasJoined(request.query.username, request.query.serverId))) {
profileId = userManager.getUserId(user.username);
value = toBase64({
"timestamp": moment().unix(),
"profileId": profileId,
"profileName": request.query.username,
"textures": {
"SKIN": {
"url": user.skinUrl ? user.skinUrl : "https://" + config.get("serverDomain") + "/images/steve.png"
}
}
});
jsonReponse = {
"id": profileId,
"name": request.query.username,
"properties": [
{
"name": "textures",
"value": value,
"signature": generateSignature(value)
}
]
};
jsonReponse = createFullProfileResponse(user, false);
logger.log(" Success");
response.json(jsonReponse);
return;
}
@@ -245,6 +224,159 @@ function sessionHasJoined(request, response) {
response.json({});
}
exports.sessionUUIDToProfile = sessionUUIDToProfile;
function sessionUUIDToProfile(request, response) {
var jsonReponse, user;
logger.log("UUID -> Profile request");
if (user = userManager.getUserByUuid(request.params.uuid)) {
jsonReponse = createFullProfileResponse(user, request.query.unsigned);
logger.log(" Success");
response.json(jsonReponse);
return;
}
logger.log(" UUID doesn't exist");
response.status(204).json({});
}
exports.apiNameToUUID = apiNameToUUID;
function apiNameToUUID(request, response) {
var userId;
logger.log("Name -> UUID request with name: " + request.params.name);
if (!request.params.name) {
logger.log(" Missing name");
response.status(400).json({
"error": "IllegalArgumentException",
"errorMessage": "Name is missing."
});
return;
}
if (userId = userManager.nameToUUID(request.params.name)) {
logger.log(" User found");
response.json({
"id": userId,
"name": request.params.name
});
return;
}
logger.log(" Name doesn't exist");
response.status(204).json({});
}
exports.apiUUIDToName = apiUUIDToName;
function apiUUIDToName(request, response) {
var user;
logger.log("UUID -> Name history request");
if (user = userManager.getUserByUuid(request.params.uuid)) {
logger.log(" UUID found");
response.json([{
"name": user.playerName
}]);
return;
}
logger.log(" UUID doesn't exist");
response.status(204).json({});
}
exports.apiPlayernamesToUUID = apiPlayernamesToUUID;
function apiPlayernamesToUUID(request, response) {
var user, userId, results = [];
logger.log("Bulk name -> UUID request");
if (typeof request.body !== 'object') {
logger.log(" Invalid list");
response.json({
"error": "IllegalArgumentException",
"errorMessage": "Invalid payload."
});
return;
}
if (Object.keys(request.body).length > 100) {
logger.log(" Too many names");
response.json({
"error": "IllegalArgumentException",
"errorMessage": "Too many names."
});
return;
}
for (var i in request.body) {
if (typeof request.body[i] === 'string' && request.body[i]) {
if (userId = userManager.nameToUUID(request.body[i])) {
user = userManager.getUserByUuid(userId);
logger.log(" User found");
results.push({
"id": userId,
"name": user.playerName
});
} else {
logger.log(" User not found");
}
} else {
response.json({
"error": "IllegalArgumentException",
"errorMessage": "Empty username."
});
return;
}
}
logger.log(" Users are fetched");
response.json(results);
}
function createFullProfileResponse(user, unsigned) {
var profileId, value, response;
profileId = userManager.getUserId(user.username);
value = {
"timestamp": moment().unix(),
"profileId": profileId,
"profileName": user.playerName,
"isPublic": true,
"textures": {
"SKIN": {
"url": "https://" + config.get("serverDomain") + "/images/skin/" + (user.skinUrl ? user.skinUrl : "steve.png")
}
}
};
if (user.capeUrl) {
value["textures"]["CAPE"] = {
"url": "https://" + config.get("serverDomain") + "/images/cape/" + user.capeUrl
}
}
value = toBase64(value);
response = {
"id": profileId,
"name": user.playerName,
"properties": [
{
"name": "textures",
"value": value
}
]
};
if (!unsigned) {
response["properties"][0]["signature"] = generateSignature(value);
}
return response;
}
function toBase64(obj) {
var buffer = new Buffer(JSON.stringify(obj));
+14 -1
View File
@@ -5,7 +5,7 @@ var users = config.get('users');
exports.findUserBy = findUserBy;
function findUserBy(field, value) {
for (var i in users) {
if (users[i][field] === value) {
if (caseInsensitiveEqual(users[i][field], value)) {
return users[i];
}
}
@@ -24,6 +24,19 @@ function saveUser(user) {
}
function caseInsensitiveEqual(left, right) {
if (left === right) {
return true;
}
if (typeof left === 'string' && typeof right === 'string' && left.toLowerCase() === right.toLowerCase()) {
return true;
}
return false;
}
// Validating UUID-s
var uuid = new RegExp("^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$");
for (var i in users) {
+38 -8
View File
@@ -52,7 +52,7 @@ function getUserId(username) {
var id = false;
findUser(username, function (user) {
id = createSelectedProfileFromId(user.id);
id = createIdFromUUID(user.id);
});
return id;
@@ -124,7 +124,7 @@ function sessionJoin(accessToken, selectedProfile, serverId) {
var success = false;
findUser(accessToken, 'accessToken', function (user, driver) {
if (selectedProfile !== createSelectedProfileFromId(user.id)) {
if (selectedProfile !== createIdFromUUID(user.id)) {
return;
}
@@ -137,10 +137,10 @@ function sessionJoin(accessToken, selectedProfile, serverId) {
}
exports.sessionHasJoined = sessionHasJoined;
function sessionHasJoined(username, serverId) {
function sessionHasJoined(playername, serverId) {
var result = false;
findUser(username, 'playerName', function (user) {
findUser(playername, 'playerName', function (user) {
if (serverId === user.serverId) {
result = user;
}
@@ -149,17 +149,43 @@ function sessionHasJoined(username, serverId) {
return result;
}
exports.nameToUUID = nameToUUID;
function nameToUUID(playername) {
var result = false;
function findUser(username, field, callback) {
findUser(playername, 'playerName', function (user) {
result = createIdFromUUID(user.id);
});
return result;
}
exports.getUserByUuid = getUserByUuid;
function getUserByUuid(uuid) {
var result = false;
findUser(createUUIDFromId(uuid), 'id', function (user) {
result = user;
});
return result;
}
function findUser(value, field, callback) {
var user;
if (!value) {
return false;
}
if (typeof field === 'function') {
callback = field;
field = 'username';
}
for (var i in drivers) {
if (user = drivers[i].findUserBy(field, username)) {
if (user = drivers[i].findUserBy(field, value)) {
callback(user, drivers[i]);
return true;
}
@@ -176,6 +202,10 @@ function makeClientToken(username) {
return crypto.createHash('md5').update(username + 'CLIENT' + new Date().getTime() + config.get('secret')).digest('hex');
}
function createSelectedProfileFromId(userId) {
return userId.replace(/-/g, '');
function createIdFromUUID(uuid) {
return uuid.replace(/-/g, '');
}
function createUUIDFromId(id) {
return id.slice(0, 8) + '-' + id.slice(8, 12) + '-' + id.slice(12, 16) + '-' + id.slice(16, 20) + '-' + id.slice(20);
}
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
+4 -2
View File
@@ -85,13 +85,15 @@ public class HostsPatcher {
private String addHosts(String file, String ip) {
return file + "\n" +
ip + " authserver.mojang.com\n" +
ip + " sessionserver.mojang.com\n";
ip + " sessionserver.mojang.com\n" +
ip + " api.mojang.com\n";
}
private String stripHosts(String file) {
return file
.replaceAll("(?m)^[0-9.]+\\s+authserver\\.mojang\\.com\\n?$", "")
.replaceAll("(?m)^[0-9.]+\\s+sessionserver\\.mojang\\.com\\n?$", "");
.replaceAll("(?m)^[0-9.]+\\s+sessionserver\\.mojang\\.com\\n?$", "")
.replaceAll("(?m)^[0-9.]+\\s+api\\.mojang\\.com\\n?$", "");
}
}
+1
View File
@@ -11,6 +11,7 @@ Features (planned):
- Own users database
- Backup authentication server (in proxy mode)
- Rate limit
In which cases are good such a system?
+22
View File
@@ -0,0 +1,22 @@
var frisby = require('frisby');
var config = require('config');
var httpPort = config.get('apiserver.httpPort');
frisby.create('Getting a non-existent user gives empty response')
.get('http://localhost:' + httpPort + '/users/profiles/minecraft/nonexistent')
.expectStatus(204)
.toss();
frisby.create('Getting a valid user gives userid and playername')
.get('http://localhost:' + httpPort + '/users/profiles/minecraft/testPlayer')
.expectStatus(200)
.expectHeaderContains('content-type', 'application/json')
.expectJSON({
"id": "650bed2c9ef54b5fb02c61fa493c68b5",
"name": "testPlayer"
})
.expectJSONTypes({
"id": String,
"name": String
})
.toss();
+117
View File
@@ -0,0 +1,117 @@
var frisby = require('frisby');
var config = require('config');
var httpPort = config.get('apiserver.httpPort');
frisby.create('Nothing gives nothing')
.get('http://localhost:' + httpPort + '/profiles/minecraft')
.expectStatus(200)
.expectHeaderContains('content-type', 'application/json')
.expectJSON([])
.toss();
frisby.create('Getting one invalid user gives nothing')
.post('http://localhost:' + httpPort + '/profiles/minecraft', [
'nonexistent'
])
.expectStatus(200)
.expectHeaderContains('content-type', 'application/json')
.expectJSON([])
.toss();
frisby.create('More than a hundred users gives error')
.post('http://localhost:' + httpPort + '/profiles/minecraft', generateNames(101))
.expectStatus(200)
.expectHeaderContains('content-type', 'application/json')
.expectJSON({
"error": "IllegalArgumentException",
"errorMessage": "Too many names."
})
.expectJSONTypes({
"error": String,
"errorMessage": String
})
.toss();
frisby.create('Getting one valid user gives userid and playername')
.post('http://localhost:' + httpPort + '/profiles/minecraft', [
'testPlayer'
])
.expectStatus(200)
.expectHeaderContains('content-type', 'application/json')
.expectJSON([{
"id": "650bed2c9ef54b5fb02c61fa493c68b5",
"name": "testPlayer"
}])
.expectJSONTypes([{
"id": String,
"name": String
}])
.toss();
frisby.create('Getting one valid user corrects invalid username case')
.post('http://localhost:' + httpPort + '/profiles/minecraft', [
'tEstPLaYer'
])
.expectStatus(200)
.expectHeaderContains('content-type', 'application/json')
.expectJSON([{
"id": "650bed2c9ef54b5fb02c61fa493c68b5",
"name": "testPlayer"
}])
.expectJSONTypes([{
"id": String,
"name": String
}])
.toss();
frisby.create('Getting an empty username gives exception')
.post('http://localhost:' + httpPort + '/profiles/minecraft', [
'testPlayer',
'',
'somebody'
])
.expectStatus(200)
.expectHeaderContains('content-type', 'application/json')
.expectJSON({
"error": "IllegalArgumentException",
"errorMessage": "Empty username."
})
.expectJSONTypes({
"error": String,
"errorMessage": String
})
.toss();
frisby.create('Getting multiple valid users gives userid and playername')
.post('http://localhost:' + httpPort + '/profiles/minecraft', [
'testPlayer',
'testPlayerOld'
])
.expectStatus(200)
.expectHeaderContains('content-type', 'application/json')
.expectJSON([
{
"id": "650bed2c9ef54b5fb02c61fa493c68b5",
"name": "testPlayer"
},
{
"id": "1294fda6159c4218be4c89b660d9cf32",
"name": "testPlayerOld"
}
])
.expectJSONTypes([{
"id": String,
"name": String
}])
.toss();
function generateNames(amount) {
var names = [];
while (names.length < amount) {
names.push('name' + names.length);
}
return names;
}
+14
View File
@@ -2,6 +2,20 @@ var frisby = require('frisby');
var config = require('config');
var httpPort = config.get('authserver.httpPort');
frisby.create('Invalid url throws not found error')
.get('http://localhost:' + httpPort + '/invalid-url-sample')
.expectStatus(404)
.expectHeaderContains('content-type', 'application/json')
.expectJSON({
"error": "Not Found",
"errorMessage": "The server has not found anything matching the request URI"
})
.expectJSONTypes({
"error": String,
"errorMessage": String
})
.toss();
frisby.create('Root path shows basic information')
.get('http://localhost:' + httpPort)
.expectStatus(200)
+1 -1
View File
@@ -1,6 +1,6 @@
var frisby = require('frisby');
var config = require('config');
var httpPort = config.get('authserver.httpPort');
var httpPort = config.get('sessionserver.httpPort');
var syncTestRunner = require('./synchronousTestRunner');
frisby.create('Nothing gives illegal argument error')
+114
View File
@@ -0,0 +1,114 @@
var frisby = require('frisby');
var config = require('config');
var moment = require('moment');
var crypto = require('crypto');
var fs = require('fs');
var httpPort = config.get('sessionserver.httpPort');
frisby.create('Getting a non-existent UUID gives empty response')
.get('http://localhost:' + httpPort + '/session/minecraft/profile/nonexistent')
.expectStatus(204)
.toss();
frisby.create('Getting a valid UUID gives player data without cape')
.get('http://localhost:' + httpPort + '/session/minecraft/profile/650bed2c9ef54b5fb02c61fa493c68b5')
.expectStatus(200)
.expectHeaderContains('content-type', 'application/json')
.expectJSON({
"id": "650bed2c9ef54b5fb02c61fa493c68b5",
"name": "testPlayer",
"properties": function (value) {
expect(value).not.toBe(undefined);
expect(value[0]).not.toBe(undefined);
expect(value[0].name).toBe('textures');
textureValueValidator(value[0].value);
signatureValidator(value[0].value, value[0].signature);
}
})
.expectJSONTypes({
"id": String,
"name": String,
"properties": function (value) {
expect(typeof value).toBe('object');
expect(typeof value[0]).toBe('object');
expect(typeof value[0].name).toBe('string');
expect(typeof value[0].value).toBe('string');
expect(typeof value[0].signature).toBe('string');
}
})
.toss();
frisby.create('Getting a valid UUID gives player data with cape')
.get('http://localhost:' + httpPort + '/session/minecraft/profile/1294fda6159c4218be4c89b660d9cf32')
.expectStatus(200)
.expectHeaderContains('content-type', 'application/json')
.expectJSON({
"id": "1294fda6159c4218be4c89b660d9cf32",
"name": "testPlayerOld",
"properties": function (value) {
var properties = base64Decode(value[0].value);
expect(typeof properties.textures.CAPE).toBe('object');
expect(typeof properties.textures.CAPE.url).toBe('string');
expect(properties.textures.CAPE.url).toBe('https://authserver.mojang.com/images/cape/mycape.png');
}
})
.toss();
frisby.create('Getting a valid UUID gives no signature if we don\'t want it')
.get('http://localhost:' + httpPort + '/session/minecraft/profile/650bed2c9ef54b5fb02c61fa493c68b5?unsigned=true')
.expectStatus(200)
.expectHeaderContains('content-type', 'application/json')
.expectJSON({
"id": "650bed2c9ef54b5fb02c61fa493c68b5",
"name": "testPlayer",
"properties": function (value) {
expect(typeof value[0].signature).toBe('undefined');
}
})
.toss();
function textureValueValidator(value) {
value = base64Decode(value);
expect(typeof value).toBe('object');
expect(typeof value.timestamp).toBe('number');
expect(Math.abs(moment().unix() - value.timestamp)).toBeLessThan(60);
expect(typeof value.profileId).toBe('string');
expect(value.profileId).toBe('650bed2c9ef54b5fb02c61fa493c68b5');
expect(typeof value.profileName).toBe('string');
expect(value.profileName).toBe('testPlayer');
expect(typeof value.isPublic).toBe('boolean');
expect(value.isPublic).toBe(true);
expect(typeof value.textures).toBe('object');
expect(typeof value.textures.SKIN).toBe('object');
expect(typeof value.textures.SKIN.url).toBe('string');
expect(value.textures.SKIN.url).toBe('https://authserver.mojang.com/images/skin/steve.png');
return true;
}
function signatureValidator(value, signature) {
var crt = fs.readFileSync('./cert/sessionserver/certificate.crt', 'utf8');
var verifier = crypto.createVerify('sha1WithRSAEncryption');
verifier.update(value);
expect(verifier.verify(crt, signature, 'base64')).toBe(true);
return true;
}
function base64Decode(str) {
var buffer = new Buffer(str, 'base64');
try {
return JSON.parse(buffer.toString('ascii'));
} catch (err) {
return null;
}
}
+2 -2
View File
@@ -3,7 +3,7 @@ var config = require('config');
var moment = require('moment');
var crypto = require('crypto');
var fs = require('fs');
var httpPort = config.get('authserver.httpPort');
var httpPort = config.get('sessionserver.httpPort');
var syncTestRunner = require('./synchronousTestRunner');
frisby.create('Nothing gives empty response')
@@ -105,7 +105,7 @@ function textureValueValidator(value) {
expect(typeof value.textures).toBe('object');
expect(typeof value.textures.SKIN).toBe('object');
expect(typeof value.textures.SKIN.url).toBe('string');
expect(value.textures.SKIN.url).toBe('https://minecraft.net/images/steve.png');
expect(value.textures.SKIN.url).toBe('https://authserver.mojang.com/images/skin/steve.png');
return true;
}
+20
View File
@@ -0,0 +1,20 @@
var frisby = require('frisby');
var config = require('config');
var httpPort = config.get('apiserver.httpPort');
frisby.create('Getting a non-existent UUID gives empty response')
.get('http://localhost:' + httpPort + '/user/profiles/nonexistent/names')
.expectStatus(204)
.toss();
frisby.create('Getting a valid user gives userid and playername')
.get('http://localhost:' + httpPort + '/user/profiles/650bed2c9ef54b5fb02c61fa493c68b5/names')
.expectStatus(200)
.expectHeaderContains('content-type', 'application/json')
.expectJSON([{
"name": "testPlayer"
}])
.expectJSONTypes([{
"name": String
}])
.toss();