1
0
Innocenty Enikeew 7 лет назад
Родитель
Сommit
9b52834044
1 измененных файлов с 45 добавлено и 8 удалено
  1. 45 8
      plugins/foursquare.lisp

+ 45 - 8
plugins/foursquare.lisp

@@ -7,6 +7,50 @@
   "URL of recent checkins API")
 (defparameter *fsq-api-url* "https://api.foursquare.com/v2/~A"
   "Foursquare API URL")
+(defparameter +fsq-oauth-url+ "https://foursquare.com/oauth2/authenticate")
+(defparameter +fsq-token-url+ "https://foursquare.com/oauth2/access_token")
+
+(defsetting *client-id* nil "App Client ID")
+(defsetting *client-secret* nil "App Client Secret")
+
+(defun get-authorization-url (chat-id)
+  (quri:render-uri
+   (quri:merge-uris
+    (quri:make-uri :query (quri:url-encode-params
+                           `(("response_type" . "code")
+                             ("client_id" . ,*client-id*)
+                             ("redirect_uri" . ,(get-oauth-url))
+                             ("state" . ,(encode-oauth-state :fsq chat-id)))))
+    (quri:uri +fsq-oauth-url+))))
+
+(def-oauth-section-handler oauth-handler (:fsq)
+  (if code
+      (progn
+        (log:info code state)
+        (let* ((resp (json-request +fsq-token-url+ :method :post
+                                   :content (list
+                                             (cons "code" code)
+                                             (cons "client_id" *client-id*)
+                                             (cons "client_secret" *client-secret*)
+                                             (cons "redirect_uri" (get-oauth-url))
+                                             (cons "grant_type" "authorization_code"))))
+               (access-token (aget "access_token" resp))
+               (chat-id (parse-integer state)))
+          (log:info chat-id access-token resp)
+          (if access-token
+              (let ((token-id (db/fsq-ensure-token access-token)))
+                (db/fsq-set-chat-token-id chat-id token-id)
+                (hunchentoot:redirect "/oauth/success"))
+              (hunchentoot:redirect "/oauth/fail"))))
+      (progn
+        (log:info error)
+        (hunchentoot:redirect "/oauth/fail"))))
+
+(defun %send-auth (chat-id)
+  (bot-send-message chat-id "Нет токена"
+                    :reply-markup (telegram-inline-keyboard-markup
+                                   (list (list (list :text "Авторизоваться!"
+                                                     :url (get-authorization-url chat-id)))))))
 
 (defun %fsq-api-call (access-token method &optional params)
   (let* ((resp
@@ -137,14 +181,7 @@
   `(let ((,token (db/fsq-get-chat-token ,chat-id)))
      (if ,token
          (progn ,@body)
-         (bot-send-message ,chat-id "Заебош токен через /fsqtoken <token>"))))
-
-(def-message-cmd-handler handle-cmd-fsqtoken (:fsqtoken)
-  (let* ((token (car args))
-         (self (fsq-fetch-self token))
-         (token-id (db/fsq-ensure-token token)))
-    (db/fsq-set-chat-token-id chat-id token-id)
-    (bot-send-message chat-id (format nil "~A, твоих друзяш будем палить теперь" (fsq-user-name self)))))
+         (%send-auth chat-id))))
 
 (def-message-cmd-handler handle-cmd-post-checkins (:postcheckins)
   (with-fsq-token (token chat-id)