auth: minor simplifications - get rid of login / login-name slot

This commit is contained in:
Helmut Merz 2024-08-20 19:28:44 +02:00
parent f6b21f9a43
commit 0766bcf418
3 changed files with 23 additions and 17 deletions

View file

@ -23,35 +23,41 @@
(let* ((auth (make-instance 'simple-authenticator)) (let* ((auth (make-instance 'simple-authenticator))
(ctx (core:default-setup cfg 'context :authenticator auth)) (ctx (core:default-setup cfg 'context :authenticator auth))
(cred (setup-credentials (admin-credentials cfg))) (cred (setup-credentials (admin-credentials cfg)))
(admin (make-principal :system :admin cred))) (admin (make-principal :system cred)))
(setf (gethash (list :system (login-name cred)) (principals auth)) admin) (setf (gethash (list :system (name cred)) (principals auth)) admin)
ctx)) ctx))
(defun setup-credentials (inp) (defun setup-credentials (inp)
(let ((pw (digest (cadr inp)))) (make-credentials (car inp) (cadr inp)))
(make-instance 'simple-credentials :login (car inp) :password pw)))
;;;; simple / basic auth service implementation ;;;; simple / basic auth service implementation
(defclass simple-authenticator () (defclass simple-authenticator ()
((principals :reader principals :initform (make-hash-table :test 'equal)))) ((principals :reader principals :initform (make-hash-table :test 'equal))))
;;; credentials
(defclass base-credentials () (defclass base-credentials ()
((login :reader login-name :initarg :login))) ((name :reader name :initarg :name)))
(defclass simple-credentials (base-credentials) (defclass simple-credentials (base-credentials)
((password :reader password :initarg :password))) ((password :reader password :initarg :password)))
(defun make-credentials (name pw &optional (cls 'simple-credentials))
(make-instance cls :name (util:to-keyword name) :password (digest pw)))
(defmethod print-object ((cred simple-credentials) stream) (defmethod print-object ((cred simple-credentials) stream)
(print-unreadable-object (cred stream :type t :identity t) (print-unreadable-object (cred stream :type t :identity t)
(format stream "~s ~s" (login-name cred) (password cred)))) (format stream "~s ~s" (name cred) (password cred))))
;;; principal (abstract / generic user object)
(defclass principal (shape:record) (defclass principal (shape:record)
((shape:head-fields :initform '(:organization :name :login)) ((shape:head-fields :initform '(:organization :name))
(shape:data-fields :initform '(:credentials :full-name :email :role)))) (shape:data-fields :initform '(:credentials :full-name :email :role))))
(defun make-principal (org name cred &optional (cls 'principal)) (defun make-principal (org cred &optional (cls 'principal))
(make-instance cls :head (list org name (login-name cred)) (make-instance cls :head (list org (name cred))
:data (list :credentials cred))) :data (list :credentials cred)))
;;;; login entry point ;;;; login entry point
@ -59,10 +65,8 @@
(defun login (inp) (defun login (inp)
(let* ((srv (core:find-service :auth)) (let* ((srv (core:find-service :auth))
(auth (authenticator srv)) (auth (authenticator srv))
(cred (make-instance 'simple-credentials (cred (make-credentials (getf inp :name) (getf inp :password)))
:login (getf inp :login) (prc (gethash (list :system (name cred)) (principals auth)))
:password (digest (getf inp :password))))
(prc (gethash (list :system (login-name cred)) (principals auth)))
(prc-cred (shape:data-value prc :credentials))) (prc-cred (shape:data-value prc :credentials)))
(util:lgi inp cred) (util:lgi inp cred)
(when (equalp (password cred) (password prc-cred)) (when (equalp (password cred) (password prc-cred))
@ -71,8 +75,8 @@
;;;; auxiliary functions ;;;; auxiliary functions
(defun digest (pw) (defun digest (pw &key (scheme :original))
(b64:encode-bytes (b64:encode-bytes
(ironclad:digest-sequence (ironclad:digest-sequence
:sha3/256 (flexi-streams:string-to-octets pw :external-format :utf8)) :sha3/256 (flexi-streams:string-to-octets pw :external-format :utf8))
:scheme :uri)) :scheme scheme))

View file

@ -8,7 +8,8 @@
:description "Authentication services" :description "Authentication services"
:depends-on (:scopes :depends-on (:scopes
:flexi-streams :ironclad :qbase64) :flexi-streams :ironclad :qbase64)
:components ((:file "auth")) :components ((:file "auth")
(:file "web" :depends-on ("auth")))
:long-description "scopes framework: authentication services." :long-description "scopes framework: authentication services."
;;#.(uiop:read-file-string ;;#.(uiop:read-file-string
;; (uiop:subpathname *load-pathname* "README.md"))) ;; (uiop:subpathname *load-pathname* "README.md")))

View file

@ -3,6 +3,7 @@
(defpackage :scopes-auth/test (defpackage :scopes-auth/test
(:use :common-lisp) (:use :common-lisp)
(:local-nicknames (:auth :scopes-auth) (:local-nicknames (:auth :scopes-auth)
(:web :scopes-auth/web)
(:client :scopes/web/client) (:client :scopes/web/client)
(:config :scopes/config) (:config :scopes/config)
(:core :scopes/core) (:core :scopes/core)
@ -32,7 +33,7 @@
(t:show-result)))) (t:show-result))))
(deftest test-login () (deftest test-login ()
(let ((cred '(:login "admin" :password "secret")) (let ((cred '(:name "admin" :password "secret"))
pr1) pr1)
(== (auth:login cred) nil) (== (auth:login cred) nil)
(setf (getf cred :password) "sc0pes") (setf (getf cred :password) "sc0pes")