Last active
January 3, 2016 04:59
-
-
Save PuercoPop/8412410 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ql:quickload :ironclad) | |
(ql:quickload :flexi-streams) | |
(defgeneric authenticate (user secret) | |
(:documentation "Authentication user with secret. Secret may be a | |
password or a token.")) | |
(defclass user () | |
((email :initarg :email :accessor email) | |
(password :initarg :password) | |
(salt :initarg :salt :initform (ironclad:make-random-salt) :reader salt))) | |
(defun new-user (email password) | |
(make-instance 'user | |
:email email | |
:password password)) | |
(defmethod initialize-instance :after ((user user) &rest initargs) | |
(declare (ignore initargs)) | |
(with-slots (password) user | |
(setf password | |
(ironclad:pbkdf2-hash-password-to-combined-string (flexi-streams:string-to-octets password))))) | |
(defmethod (setf password) (new-value (user user)) | |
(with-slots (password) user | |
(setf password (ironclad:pbkdf2-hash-password-to-combined-string (flexi-streams:string-to-octets new-value))))) | |
(defmethod password ((user user)) | |
(slot-value user 'password)) | |
(defmethod authenticate ((user user) password) | |
(ironclad:pbkdf2-check-password (flexi-streams:string-to-octets password) (password user))) | |
;; Example | |
(defvar *user* (new-user "foo" "ohhai")) | |
(authenticate *user* "ohhai") ; => t | |
(setf (password *user*) "hai") | |
(authenticate *user "ohhai") ; => nil | |
(authenticate *user "hai") ; => t | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment