Skip to content
This repository was archived by the owner on Sep 23, 2021. It is now read-only.

Commit 62c257f

Browse files
author
Anton Pirogov
committed
first commit
0 parents  commit 62c257f

File tree

7 files changed

+287
-0
lines changed

7 files changed

+287
-0
lines changed

Diff for: .gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
passwords.txt
2+
.rvmrc

Diff for: README

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
MCMyAuth - Custom Minecraft User Authentication
2+
Copyright (C) 2011 Anton Pirogov
3+
Licensed under the GPL version 3
4+
5+
----
6+
Why MCMyAuth?
7+
8+
It is lightweight, easy to set up (on unix machines)
9+
and provides an alternative login system independent
10+
from www.minecraft.net for your personal server.
11+
MCMyAuth only requires to add an entry to your hosts file
12+
and needs no server side modding.
13+
14+
----
15+
Why NOT MCMyAuth?
16+
17+
This program handles just the basics and provides password
18+
secured login for your users. You can not set skins or cloaks
19+
and there is no password recovery system (admin can only delete
20+
user from the file to let him register again)
21+
22+
Also, MCMyAuth depends on the usage of a modded Minecraft Launcher,
23+
which allows to decide which authentication server to connect to
24+
(that means, with that launcher a player can connect to any
25+
MCMyAuth server with a provided address).
26+
27+
----
28+
Requirements: sinatra gem
29+
30+
----
31+
Usage:
32+
1. redirect www.minecraft.net to the server running this app in your hosts file.
33+
This is neccessary for the server to use this app instead of the official
34+
site without modding the minecraft_server.jar.
35+
36+
2. start the app with start.sh (before you might want to change some settings in config.rb)
37+
38+
3. run your server with online-mode=true
39+
40+
Now all authentication should run over your own authentication server (this app).
41+
Your players can now login over the MCMyAuth launcher.
42+
43+
You can open localhost/users in your webbrowser to register accounts.
44+
(If you have not modified the URL)
45+

Diff for: config.rb

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#MCMyAuth configuration settings
2+
3+
#relative URL of the main page of MCMyAuth
4+
$mainpage = '/users'
5+
6+
#name of your game server / clan / whatever
7+
$servername = 'New Server'
8+
9+
#address of your game server (which you connect to for SMP)
10+
$serveraddress = 'localhost'
11+
12+
#contact information of server admin (forgotten passwords etc)
13+
$admincontact = 'E-Mail: admin@server.com'
14+

Diff for: main.rb

+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
#!/usr/bin/env ruby
2+
#MCMyAuth - Custom Minecraft User Authentication
3+
#Copyright (C) 2011 Anton Pirogov
4+
#Licensed under the GPL version 3
5+
require 'rubygems'
6+
require 'sinatra'
7+
require 'erb'
8+
require 'digest/md5'
9+
10+
require './config' if File.exists?('./config.rb')
11+
12+
#set defaults if not present in config
13+
$mainpage ||= '/users'
14+
$servername ||= '<$servername not set>'
15+
$serveraddress ||= '<$serveraddress not set>'
16+
$admincontact ||= '<$admincontact not set>'
17+
18+
19+
$mcma_version = 1 #MCMyAuth program version
20+
$current_launcher_version = 1
21+
$pwpath = 'passwords.txt' #File where password md5s are stored
22+
23+
$userpws = Hash.new
24+
$sessions = Hash.new
25+
$serverids = Hash.new
26+
27+
def md5(str)
28+
Digest::MD5.hexdigest str
29+
end
30+
31+
def load_users
32+
userhashs = Hash.new
33+
34+
return Hash.new if File.exists?($pwpath)==false
35+
lines = File.open($pwpath,'r').readlines.map{|l| l.chomp}
36+
lines.each{|l| userhashs[l.split('=')[0]] = l.split('=')[1] }
37+
38+
$userpws = userhashs
39+
end
40+
41+
def save_users
42+
File.rename($pwpath, $pwpath+'.bak') if File.exists?($pwpath)
43+
44+
f = File.open($pwpath,'w')
45+
$userpws.each do |k,v|
46+
f.puts k+"="+v
47+
end
48+
f.close
49+
50+
File.delete($pwpath+'.bak') if File.exists?($pwpath+'.bak')
51+
52+
return true
53+
end
54+
55+
#render a message using erb and providing a back link
56+
def msg(str)
57+
erb str + '<br /><a href="'+$mainpage+'">back</a>'
58+
end
59+
60+
# Following is called from the website
61+
62+
get $mainpage do
63+
erb :index
64+
end
65+
66+
post $mainpage+'/register' do
67+
name = params[:name]
68+
pwd = params[:pwd]
69+
return msg 'You have to set a nickname!' if name==''
70+
return msg 'User already exists!' if $userpws[name] != nil
71+
return msg 'You have to set a password!' if pwd==''
72+
73+
$userpws[name] = md5 pwd
74+
save_users
75+
76+
msg 'User successfully registered!<br /><br />You can login using the MCMyAuth launcher with Server=<b>'+$serveraddress+'</b> and your new account!'
77+
end
78+
79+
post $mainpage+'/changepw' do
80+
name = params[:name]
81+
pwd = params[:pwd]
82+
return msg 'Wrong nickname or password!' if $userpws[name].nil? || md5(pwd) != $userpws[name]
83+
84+
$userpws[name] = md5 params[:newpwd]
85+
save_users
86+
87+
msg 'Password changed!'
88+
end
89+
90+
post $mainpage+'/unregister' do
91+
name = params[:name]
92+
pwd = params[:pwd]
93+
return msg 'Wrong nickname or password!' if $userpws[name].nil? || md5(pwd) != $userpws[name]
94+
95+
$userpws.delete(name)
96+
save_users
97+
98+
msg 'Account deleted!'
99+
end
100+
101+
102+
# Called by client
103+
get '/game/joinserver.jsp' do
104+
name = params[:user]
105+
serverid = params[:serverId].to_s
106+
return 'User not registered!' if $userpws[name].nil?
107+
return 'User not authenticated!' if $sessions[name] != params[:sessionId]
108+
109+
#if hash passed -> name verification
110+
if serverid != ""
111+
$serverids[name] = serverid
112+
end
113+
114+
'ok'
115+
end
116+
117+
# Called by server
118+
get '/game/checkserver.jsp' do
119+
name = params[:user]
120+
serverid = params[:serverId]
121+
return 'Failed to verify username!' if $userpws[name].nil? || serverid != $serverids[name]
122+
123+
'YES'
124+
end
125+
126+
# Called by the launcher
127+
post '/login' do
128+
name = params[:user]
129+
pwd = params[:password]
130+
version = params[:version]
131+
return 'Wrong nickname or password!' if $userpws[name].nil? || md5(pwd) != $userpws[name]
132+
return 'Outdated launcher!' if version.to_i < $current_launcher_version
133+
134+
#generate new session id
135+
sid = ''
136+
16.times{ sid += rand(16).to_s(16) }
137+
$sessions[name] = sid
138+
139+
#return nick + sessionid
140+
return params[:user]+':'+sid
141+
end
142+
143+
#Init user hash
144+
load_users

Diff for: start.sh

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rvmsudo ruby main.rb -p80

Diff for: views/index.erb

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<h2>Welcome to the user management of <%= $servername %></h2>
2+
3+
<div class="box">
4+
<form method="post" action="<%=$mainpage%>/register">
5+
<p style="font-weight: bold;">Create account</p>
6+
<label for="name">Nickname:</label><input type="text" name="name" maxlength="20" />
7+
<br />
8+
<label for="pwd">Password:</label><input type="password" name="pwd" maxlength="20" />
9+
<label>&nbsp;</label><input type="submit" value="Register" />
10+
</form>
11+
</div>
12+
13+
<div class="box">
14+
<form method="post" action="<%=$mainpage%>/changepw">
15+
<p style="font-weight: bold;">Change password</p>
16+
<label for="name">Nickname:</label><input type="text" name="name" maxlength="20" />
17+
<br />
18+
<label for="pwd">Old password:</label><input type="password" name="pwd" maxlength="20" />
19+
<label for="newpwd">New password:</label><input type="password" name="newpwd" maxlength="20" />
20+
<label>&nbsp;</label><input type="submit" value="Change password" />
21+
</form>
22+
</div>
23+
24+
<div class="box">
25+
<form method="post" action="<%=$mainpage%>/unregister">
26+
<p style="font-weight: bold;">Delete account</p>
27+
<label for="name">Nickname:</label><input type="text" name="name" maxlength="20" />
28+
<br />
29+
<label for="pwd">Password:</label><input type="password" name="pwd" maxlength="20" />
30+
<label>&nbsp;</label><input type="submit" value="Unregister" />
31+
</form>
32+
</div>
33+
34+
<br /><br />
35+
Forgot your password? Contact the administrator:<br/>
36+
<div class="box">
37+
<%= $admincontact %>
38+
</div>
39+

Diff for: views/layout.erb

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<html>
2+
<head>
3+
<title><%= $servername %></title>
4+
<style type="text/css">
5+
body {
6+
font-family: sans-serif;
7+
}
8+
9+
.box {
10+
border: 3px dashed gray;
11+
margin: 5px;
12+
padding: 5px;
13+
width: 30%;
14+
}
15+
.box form {
16+
padding: 0px;
17+
margin: 0px;
18+
}
19+
.box p {
20+
padding: 0px;
21+
margin: 0px;
22+
margin-bottom: 10px;
23+
}
24+
.box label {
25+
float: left;
26+
width: 40%;
27+
}
28+
29+
#bottom {
30+
float: right;
31+
color: gray;
32+
font-size: 10px;
33+
}
34+
</style>
35+
</head>
36+
<body>
37+
<%= yield %>
38+
<span id="bottom">
39+
Powered by MCMyAuth v<%=$mcma_version%>
40+
</span>
41+
</body>
42+
</html>

0 commit comments

Comments
 (0)