Skip to content

how do I add a new provider #4

@gassechen

Description

@gassechen

I created a file azure.com.lisp

(in-package #:saluto)

(defclass oauth2-azure.com (oauth2-provider)
  ((oauth-login-url
    :initform "https://login.onmicrosoft.com/b2c_1_login/oauth2/v2.0/authorize"
    :allocation :class)
   (access-token-query-url
    :initform "https://login.onmicrosoft.com/webapivehiculos/user_impersonation"
    :allocation :class)
   (userinfo-query-url
    :initform "https://login.onmicrosoft.com/webapivehiculos/user_impersonation"
    :allocation :class)))

(defmethod make-redirect-uri ((provider oauth2-azure.com) session redirect-uri)
  (declare (ignore session redirect-uri))
  (restas:genurl* 'receiver-route
                  :provider (name provider)
                  :states ""))

(defmethod build-goto-path :around ((provider oauth2-azure.com)
                                    session
                                    redirect-uri)
  (append
   (call-next-method provider session redirect-uri)
   (list
    "response_type" "code"
    "scope" "https://mtkb2c.onmicrosoft.com/api/user_impersonation/openid/profile/offline_access"
    "state" (make-state session redirect-uri))))

(defmethod prepare-access-token-request :around ((provider
                                                  oauth2-azure.com)
                                                 code
                                                 goto-path)
  "Azure needs parameters to be send as data"
  (let ((request (call-next-method provider code goto-path)))
    (setf (getf (cdr request) :parameters)
          (concatenate-params (cons
                               '("grant_type" . "authorization_code")
                               (getf (cdr request) :parameters))))
    (substitute :content :parameters request)))

(defun remove-zeros-from-string (array)
  "This function is needed by azure.com provider,
because the answer of azure.com for unknown reasons contains sudden chunks of zeros."
  (coerce (loop for x across array unless (zerop x)
               collect (code-char x))
          'string))

(defmethod extract-access-token :around ((provider oauth2-azure.com)
                                         answer)
  (call-next-method provider (remove-zeros-from-string answer)))

(defmethod extract-userinfo :around ((provider oauth2-azure.com)
                                     answer)
  (call-next-method provider (remove-zeros-from-string answer)))

(defmethod extract-userinfo ((provider oauth2-azure.com)
                             parsed-answer)
  (labels ((code-decode (string)              ;;;; Indeed, I don't know what does it mean
             (babel:octets-to-string
              (babel:string-to-octets
               string
               :encoding :LATIN-1)
              :encoding :UTF-8)))
    (list :first-name (code-decode (json-val parsed-answer "given_name"))
          :last-name (code-decode (json-val parsed-answer "family_name"))
          :avatar (json-val parsed-answer "picture")
          :email (json-val parsed-answer "email")
          :uid (json-val parsed-answer "id"))))

and

(:defsystem saluto
  :name "Saluto"
  :author "Dmitry Solomennikov <dmitrys99@mail.ru>"
  :version "0.0.1"
  :description "OAuth 2.0 authentication for RESTAS"
  :depends-on (#:hunchentoot
               #:restas
               #:ironclad
               #:babel
               #:split-sequence
               #:jsown
               #:cl-ppcre
               #:drakma)
  :serial t
  :components ((:file "package")
               (:file "utils")
               (:file "provider")
               (:file "routes")
               (:module "providers"
                :components ((:file "facebook.com")
                             (:file "github.com")
                             (:file "google.com")
                             (:file "mail.ru")
			     (:file "azure.com")
                             (:file "vk.com")
			     (:file "ok.ru")))))

and

(restas:define-module` #:saluto
  (:use #:cl)
  (:export #:*main*
           #:*store-userinfo-fun*
           #:*logged-in-p-fun*
           #:*logout-fun*
           #:*providers*
           #:oauth2-facebook.com
           #:oauth2-github.com
           #:oauth2-google.com
           #:oauth2-mail.ru
           #:oauth2-vk.com
	   #:oauth2-azure.com
	   #:oauth2-ok.ru))

but it gives me this error
Not Found
The requested URL /auth/goto/azure.com/ was not found on this server.
what am I doing wrong?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions