From cb7069f8a55508267bcdd2d3afb61ed598f045a1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andrej=20Rama=C5=A1euski?= <andrej@x2.cz>
Date: Wed, 23 Dec 2020 20:16:53 +0100
Subject: [PATCH] Pridan user.secret

---
 lib/CF.pm                    |  2 ++
 lib/CF/Schema/Result/User.pm | 17 +++++++++++++++++
 openapi.yaml                 |  2 ++
 sql/1/up.sql                 |  1 +
 4 files changed, 22 insertions(+)

diff --git a/lib/CF.pm b/lib/CF.pm
index a1fb659..93e3429 100644
--- a/lib/CF.pm
+++ b/lib/CF.pm
@@ -69,6 +69,8 @@ sub startup {
                 my $user = $c->schema->resultset('User')->find_or_create(
                     $c->user, { key => 'uuid'}
                 );
+                $user->set_secret();
+
                 $c->stash->{user}{id} = $user->id;
 
                 return $c->$cb() if ! scalar @{ $scopes };
diff --git a/lib/CF/Schema/Result/User.pm b/lib/CF/Schema/Result/User.pm
index 3bf40ca..5f0207d 100644
--- a/lib/CF/Schema/Result/User.pm
+++ b/lib/CF/Schema/Result/User.pm
@@ -5,6 +5,8 @@ use warnings;
 
 use base 'DBIx::Class::Core';
 
+use Data::Random qw(rand_chars);
+
 our $VERSION = 1;
 
 __PACKAGE__->table('users');
@@ -20,6 +22,7 @@ __PACKAGE__->add_columns(
         uuid
         is_active
         username
+        secret
         name
         main_group_name
         keepalive
@@ -51,4 +54,18 @@ sub formatted {
 
 }
 
+sub set_secret {
+    my $self = shift;
+    my $new  = shift;
+
+    return if $self->secret and not $new;
+
+    my $secret = rand_chars( set => 'alphanumeric', size => 16 );
+
+    $self->update({
+        secret => $secret
+    });
+
+}
+
 1;
diff --git a/openapi.yaml b/openapi.yaml
index 4915664..79731d4 100644
--- a/openapi.yaml
+++ b/openapi.yaml
@@ -114,6 +114,8 @@ components:
             type: string
         username:
             type: string
+        secret:
+            type: string
         is_banned:
             type: boolean
     Announcement:
diff --git a/sql/1/up.sql b/sql/1/up.sql
index b7e7770..76c7bda 100644
--- a/sql/1/up.sql
+++ b/sql/1/up.sql
@@ -19,6 +19,7 @@ create table "users" (
     "uuid" varchar(36) not null,
     "is_active" bool not null default 'true',
     "username" text,
+    "secret" text,
     "name" text,
     "main_group_name" text,
     "keepalive" timestamp,
-- 
GitLab