;;; gh-oauth.el --- oauth module for gh.el

;; Copyright (C) 2012  Yann Hodique

;; Author: Yann Hodique <yann.hodique@gmail.com>
;; Keywords:

;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;;; Commentary:

;;

;;; Code:

(eval-when-compile
  (require 'cl))

;;;###autoload
(require 'eieio)

(require 'gh-api)
(require 'gh-auth)
(require 'gh-common)

;;;###autoload
(defclass gh-oauth-api (gh-api-v3)
  ((auth-cls :allocation :class :initform gh-oauth-authorization))
  "OAuth API")

(defclass gh-oauth-password-authenticator (gh-password-authenticator)
  ((remember :allocation :class :initform nil)))

(defmethod initialize-instance ((api gh-oauth-api) &rest args)
  ;; force password authentication for this API
  (let ((gh-api-v3-authenticator 'gh-oauth-password-authenticator))
    (call-next-method)))

(defclass gh-oauth-authorization (gh-object)
  ((id :initarg :id)
   (url :initarg :url)
   (scopes :initarg :scopes)
   (token :initarg :token)
   (app :initarg :app :initform nil)
   (updated-at :initarg :updated-at)
   (created-at :initarg :created-at)

   (app-cls :allocation :class :initform gh-oauth-app)))

(defmethod gh-object-read-into ((auth gh-oauth-authorization) data)
  (call-next-method)
  (with-slots (id url scopes token app updated-at created-at)
      auth
    (setq id (gh-read data 'id)
          url (gh-read data 'url)
          scopes (gh-read data 'scopes)
          token (gh-read data 'token)
          app (gh-object-read (or (oref auth :app)
                                  (oref auth app-cls))
                              (gh-read data 'app))
          updated-at (gh-read data 'updated_at)
          created-at (gh-read data 'created_at))))

(defclass gh-oauth-app (gh-object)
  ((url :initarg :url)
   (name :initarg :name)))

(defmethod gh-object-read-into ((app gh-oauth-app) data)
  (call-next-method)
  (with-slots (url name)
      app
    (setq url (gh-read data 'url)
          name (gh-read data 'name))))

(defmethod gh-oauth-auth-list ((api gh-oauth-api))
  (gh-api-authenticated-request
   api (gh-object-list-reader (oref api auth-cls)) "GET"
   (format "/authorizations")))

(defmethod gh-oauth-auth-get ((api gh-oauth-api) id)
  (gh-api-authenticated-request
   api (gh-object-reader (oref api auth-cls)) "GET"
   (format "/authorizations/%s" id)))

(defmethod gh-oauth-auth-new ((api gh-oauth-api) &optional scopes)
  (gh-api-authenticated-request
   api (gh-object-reader (oref api auth-cls)) "POST"
   (format "/authorizations") (list (cons 'scopes scopes)
                                    (cons 'note (format "gh.el - %s"
                                                        (system-name))))))

(defmethod gh-oauth-auth-update ((api gh-oauth-api) id &optional scopes)
  (gh-api-authenticated-request
   api (gh-object-reader (oref api auth-cls)) "PATCH"
   (format "/authorizations/%s" id) (list (cons 'scopes scopes))))

(defmethod gh-oauth-auth-delete ((api gh-oauth-api) id)
  (gh-api-authenticated-request
   api nil "DELETE" (format "/authorizations/%s" id)))

(provide 'gh-oauth)
;;; gh-oauth.el ends here

;; Local Variables:
;; indent-tabs-mode: nil
;; End:
