Skip to content

Commit 863221f

Browse files
author
Christian Kruse
committed
FEAT: add a user authentication token for websocket connections
1 parent ed06be5 commit 863221f

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

app/models/cf_user.rb

+6
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ def self.find_first_by_auth_conditions(conditions = {})
4848
end
4949
end
5050

51+
def after_database_authentication
52+
if websocket_token.blank?
53+
update_attributes(websocket_token: SecureRandom.uuid)
54+
end
55+
end
56+
5157
def has_badge?(type)
5258
badges.each do |b|
5359
return true if b.badge_type == type
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# -*- coding: utf-8 -*-
2+
3+
class AddWebsocketToken < ActiveRecord::Migration
4+
def up
5+
execute <<-SQL
6+
ALTER TABLE users ADD COLUMN websocket_token CHARACTER VARYING(250) UNIQUE;
7+
SQL
8+
end
9+
10+
def down
11+
execute <<-SQL
12+
ALTER TABLE users DROP COLUMN websocket_token;
13+
SQL
14+
end
15+
end
16+
17+
# eof

db/structure.sql

+12-1
Original file line numberDiff line numberDiff line change
@@ -1324,7 +1324,8 @@ CREATE TABLE users (
13241324
avatar_file_name character varying(255),
13251325
avatar_content_type character varying(255),
13261326
avatar_file_size integer,
1327-
avatar_updated_at timestamp without time zone
1327+
avatar_updated_at timestamp without time zone,
1328+
websocket_token character varying(250)
13281329
);
13291330

13301331

@@ -1793,6 +1794,14 @@ ALTER TABLE ONLY users
17931794
ADD CONSTRAINT users_pkey PRIMARY KEY (user_id);
17941795

17951796

1797+
--
1798+
-- Name: users_websocket_token_key; Type: CONSTRAINT; Schema: cforum; Owner: -; Tablespace:
1799+
--
1800+
1801+
ALTER TABLE ONLY users
1802+
ADD CONSTRAINT users_websocket_token_key UNIQUE (websocket_token);
1803+
1804+
17961805
--
17971806
-- Name: votes_pkey; Type: CONSTRAINT; Schema: cforum; Owner: -; Tablespace:
17981807
--
@@ -2617,6 +2626,8 @@ INSERT INTO schema_migrations (version) VALUES ('54');
26172626

26182627
INSERT INTO schema_migrations (version) VALUES ('55');
26192628

2629+
INSERT INTO schema_migrations (version) VALUES ('56');
2630+
26202631
INSERT INTO schema_migrations (version) VALUES ('6');
26212632

26222633
INSERT INTO schema_migrations (version) VALUES ('7');

0 commit comments

Comments
 (0)