summaryrefslogtreecommitdiff
path: root/www/static/js/auth.js
blob: bb2d3042b14ee371c2afef0fc80362a907ace099 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
var Auth =
	{
	userid: false,
	username: false,
	session: false,
	loaded: false,
	access: 0,
	login: function ()
		{
		d.warn("LOG IN")
		var username = d.trim( $("#login-username").val() )
		var password = d.trim( $("#login-password").val() )
		var pwhash = $.md5("scanjam"+password)
		if (! username || ! password) return
		Main.enter = false
		d.warn("LOGGING IN")
		$.post(API.URL.auth.login, {'username':username, 'password': pwhash}, Auth.loginCallback)
		$("#chat").hide()
		},
	loginCallback: function (raw)
		{
		var lines = API.parse("/auth/login", raw)
		if (! lines.length) return
		if (lines[0] !== "OK")
			{
			alert(lines[0].split("\t")[1])
			return Auth.error()
			}
		u = lines[1].split("\t")

		Auth.userid = u[0]
		Auth.username = u[1]
		Auth.session = u[2]
		Auth.access = u[3]

		document.cookie = "session="+Auth.session+";path=/;domain=.scannerjammer.fm;max-age=1086400"
		Auth.success()
		},
	checkin: function ()
		{
		d.warn("CHECK IN")
		$.post(API.URL.auth.checkin, {'session':Auth.session}, Auth.checkinCallback)
		},
	checkinCallback: function (raw)
		{
		var lines = API.parse("/auth/checkin", raw)
		if (! lines.length) return
		if (lines[0] !== "OK")
			{
			alert(lines[0].split("\t")[1])
			return Auth.error()
			}
		u = lines[1].split("\t")
		Auth.userid = u[0]
		Auth.username = u[1]
		Auth.success()
		},
	sneakin: function (userid,username)
		{
		d.warn("SNEAK IN")
		$.post(API.URL.auth.sneakin, {'userid':userid,'username':username}).success(Auth.sneakinCallback)
		},
	sneakinCallback: function (raw)
		{
		var lines = API.parse("/auth/sneakin", raw)
		if (! lines.length) return
		if (lines[0] !== "OK")
			{
			alert(lines[0].split("\t")[1])
			return Auth.error()
			}
		d.joy("snuck in!")
		u = lines[1].split("\t")

		Auth.userid = u[0]
		Auth.username = u[1]
		Auth.session = u[2]
		Auth.access = u[3]

		d.warn(lines[1])
		if (! Auth.session)
			return
		document.cookie = "session="+Auth.session+";path=/;domain=.scannerjammer.fm;max-age=1086400"
		Auth.success()
		},
	logout: function ()
		{
		d.warn("LOG OUT")
		clearTimeout(Room.timer)
		Room.unload()
		Auth.userid = false
		Auth.username = false
		Local.set('userid', false)
		Local.set('username', false)
		document.cookie = "session=false;path=/;domain=.scannerjammer.fm;max-age=0"
		Auth.session = ""
		Auth.load()
		},
	error: function ()
		{
		Auth.load()
		},
	success: function ()
		{
		d.joy("logged in as "+Auth.username)
		Auth.unload()
		Room.load()
		},
	unload: function ()
		{
		d.warn("AUTH UNLOAD")
		$("#login").hide()
		$("#loading").show()
		Keyboard.enter = false
		Auth.loaded = false
		},
	load: function ()
		{
		d.warn("AUTH LOAD")
		$("#loading").hide()
		$("#login").show()
		$("#login-username").focus()
		$("#login-username").keydown(Keyboard.textareaMap)
		$("#login-password").keydown(Keyboard.textareaMap)
		$("#login-password").val("")
		$("#login-go").click(Auth.login)
		Keyboard.enter = Auth.login
		$("#bg").show()
		Auth.loaded = true
		},
	init: function ()
		{
		d.warn("INIT AUTH")
		if (document.cookie)
			{
			d.warn("got a cookie")
			d.warn(document.cookie)
			var cookies = document.cookie.split(";")
			for (i in cookies)
				{
				var cookie = cookies[i].split("=")
				if (cookie[0].indexOf("session") !== -1)
					{
					if (cookie[1] !== 'false' && cookie[1] !== 'undefined')
						{
						Auth.session = cookie[1]
						break
						}
					}
				}
			d.warn("got sessionid "+Auth.session)
			if (Auth.session)
				return true
			}
		var userid = Local.get('userid')
		var username = Local.get('username')
		if (userid && username)
			{
			d.warn("attempting to sneak in "+username)
			Auth.sneakin(userid,username)
			return true
			}
		return false
		}
	}