From 377903f7a1225d68ed650484556d047bdee49b50 Mon Sep 17 00:00:00 2001 From: Evan Reichard Date: Sun, 17 Jan 2021 23:56:56 -0500 Subject: [PATCH] (WIP) Refresh & Access --- cmd/imagini.db | Bin 73728 -> 0 bytes cmd/server/server.go | 8 +- go.mod | 2 + go.sum | 30 ++++++++ internal/api/api.go | 6 +- internal/api/auth.go | 44 ++++++++--- internal/api/middlewares.go | 45 +++++++---- internal/api/routes.go | 41 +++++++--- internal/api/users.go | 4 +- internal/auth/auth.go | 149 ++++++++++++++++++++++++++++++++++-- internal/db/db.go | 69 +---------------- internal/db/devices.go | 44 +++++++++++ internal/models/db.go | 39 ++++++---- internal/session/session.go | 21 ++++- 14 files changed, 366 insertions(+), 136 deletions(-) delete mode 100644 cmd/imagini.db create mode 100644 internal/db/devices.go diff --git a/cmd/imagini.db b/cmd/imagini.db deleted file mode 100644 index eca2661838d0f1c68e8b10fbf0358ad349b79375..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 73728 zcmeI*&rjQC7{GD6DVR_KZKulWVVh}-)~IQlz<{<=r7;@1j1WplrnQH~Zeze*ez3$T zblN4XyYI07W%u27*#EKn4!!KK*LFy3haZ}@Y8w7VggAa*`}O;I9=~?<2C=!3w`^0} zYPRZzt<6anC0Uj}(KJbt65@GTJP#ieV&VMZ8}TapmXBLZNOx9$OibF+rIC^}6`%ZW z;^)cS>{w@ zNiny!RD7zfW}gmi)vujQEw2|gOU0#Jp`__sUsncg^%k9mbZuq5n9V&d9J#ZkR?Mzs zi`l|*cJuIHovqsXO53uI$#bs{5{ZrE1np+pJa$+iBrqk3Cm-l>Jf@$39d`*VYS~ zBkdf`U}HVmot{+GD_7)$88@g-4d*!(45YiE2u2`oFIuMI9fPh_#gc8+&6M8Rb(WnY z^RM+a>8sl?>ZY#Q=Dw{J)=OHUoX@9%(Rad-JlL2})R`Iijp<7~qxQU0@1>r%oCqGX zhv*fZlzwiFzmb6b`p4vl*Wx=)!d@fpOE?h+2N=k2Z-QR>eZ|ALDCJnSI7H(AsFc1=KooG zej8WRrR#F{{l18odhSr-J5zmY$A@ivrM_)EAYWNX>0oC}QLkT@e^7m=hTE`jn|RR6 zSK^!(|MEF)?ps?GX9)10{x!q4>`t|JvZwS~v#}jm;T@CxdN({CXpgchOXYk?yY^(K zX*aK>^p;tx>>BpY=>x~nmk2K`ownHupE%B76F6m@O8w#cxuU4^s@xsvhuJOm1Aopq~@CeNQ8(tgphc5SQK2t_}ozcOl_@DOp-EratRUwr82fdB#sAbdNy-Aow=P^&@u}TK3;r~p1-p= zyD%qKrRN`{)2Edi)wUEXeOY<7%0cUQ`0x%q72 zp1GP{-J9FDAKx!LGe4{4JBzFJt@2)ZbKRN$m#6-e#19Vy5I_I{1Q0*~0R#|0009IL zID-PQSR(Fy{(mb?y*+~+q)P}OfB*srAb UUID? type User struct { - gorm.Model + Base Email string `json:"email" gorm:"unique"` Username string `json:"username" gorm:"unique"` FirstName string `json:"first_name"` LastName string `json:"last_name"` AuthType string `json:"auth_type" gorm:"default:Local;not null"` Password string `json:"-"` - JWTSecret string `json:"-" gorm:"unique;not null"` // TODO: Auto Generate UUID } type MediaItem struct { - gorm.Model - User User `json:"user" gorm:"ForeignKey:ID;not null"` + Base + User User `json:"user" gorm:"ForeignKey:UUID;not null"` EXIFDate time.Time `json:"exif_date"` Latitude string `json:"latitude"` Longitude string `json:"longitude"` @@ -45,11 +58,11 @@ type MediaItem struct { } type Tag struct { - gorm.Model + Base Name string `json:"name" gorm:"not null"` } type Album struct { - gorm.Model + Base Name string `json:"name" gorm:"not null"` } diff --git a/internal/session/session.go b/internal/session/session.go index 38d0ba6..ea001c9 100644 --- a/internal/session/session.go +++ b/internal/session/session.go @@ -4,8 +4,11 @@ import ( "sync" ) +// Used to maintain a cache of user specific jwt secrets +// This will prevent DB lookups on every request type SessionManager struct { - Mutex sync.Mutex + mutex sync.Mutex + values map[string]string } func NewMgr() *SessionManager { @@ -13,13 +16,23 @@ func NewMgr() *SessionManager { } func (sm *SessionManager) Set(key, value string) { - + sm.mutex.Lock() + sm.values[key] = value + sm.mutex.Unlock() } func (sm *SessionManager) Get(key string) string { - return "" + sm.mutex.Lock() + defer sm.mutex.Unlock() + return sm.values[key] } func (sm *SessionManager) Delete(key string) { - + sm.mutex.Lock() + defer sm.mutex.Unlock() + _, exists := sm.values[key] + if !exists { + return + } + delete(sm.values, key) }