developer's diary

最近はc#のエントリが多いです

サイボウズliveのOAuthを利用してredmineにログインするプラグイン

以下サイトを参考に作成しました。

※事前にRedmineでユーザを作成しておく必要があります。

環境周り

バージョン:Redmine 1.1.2.stable (MySQL)

$ RAILS_ENV=production script/about
About your application's environment
Ruby version              1.8.7 (i686-linux)
RubyGems version          1.4.2
Rack version              1.1.2
Rails version             2.3.11
Active Record version     2.3.11
Active Resource version   2.3.11
Action Mailer version     2.3.11
Active Support version    2.3.11
Application root          /home/redmine/redmine-1.1.2
Environment               production
Database adapter          mysql
Database schema version   20101114115359

プラグイン作成

redmineのプラグインコントローラだけ作成すれば動くようです。

#gemでoauthライブラリをインストール
$ gem install oauth

#プラグイン用ディレクトリ作成
$ mkdir -p vendor/plugins/redmine_cybozulive_auth/app/controllers

#コントローラ記述
vim $ vendor/plugins/redmine_cybozulive_auth/app/controllers/cybozulive_controller.rb

↓cybozulive_controller.rb

require 'oauth'
require 'rexml/document'

class CybozuliveController < AccountController
  unloadable

  CONSUMER_KEY = "CONSUMER_KEY を記述"
  CONSUMER_SECRET = "CONSUMER_SECRET を記述"

  def self.consumer
    OAuth::Consumer.new(
      CONSUMER_KEY,
      CONSUMER_SECRET,
      { :site => "https://api.cybozulive.com",
        :request_token_url => "https://api.cybozulive.com/oauth/initiate",
        :access_token_url => "https://api.cybozulive.com/oauth/token"
      }
    )
  end

  #ログイン画面呼び出し
  def index
    #リクエストトークンの取得
    consumer = CybozuliveController.consumer
    request_token = consumer.get_request_token()
    session[:request_token] = request_token.token
    session[:request_token_secret] = request_token.secret
    #サイボウズliveのログイン画面にリダイレクト
    redirect_to request_token.authorize_url
  end

  #サイボウズLiveからのコールバックURLで実行されるメソッド
  def callback
    #アクセストークンの取得
    consumer = CybozuliveController.consumer
    request_token = OAuth::RequestToken.new(
      consumer,
      session[:request_token],
      session[:request_token_secret]
    )
    access_token = request_token.get_access_token(
        :oauth_verifier => params[:oauth_verifier]
    )

    #サイボウズLiveのAPIを実行
    response = consumer.request(
      :get,
      '/api/notification/V2',
      access_token, { :scheme => :query_string }
    )

    case response
    when Net::HTTPSuccess
      #APIの実行に成功

      #結果のXMLをオブジェクトに変換
      user_Rexml = REXML::Document.new(response.body)
      unless user_Rexml.elements['feed/author/email']
        #メールアドレスの取得に失敗(未確認ロジック)
        flash[:notice] = "Authentication failed"
        redirect_to :action => :index
        return
      end
    else
      #HTTPアクセス失敗(未確認ロジック)
      RAILS_DEFAULT_LOGGER.error "Failed to get user info via OAuth"
      flash[:notice] = "Authentication failed"
      redirect_to :action => :index
      return
    end

    #サイボウズAPIの結果よりメールアドレス抽出
    mail = user_Rexml.elements['feed/author/email'].get_text
    #メールアドレスを元にユーザを検索
    user = User.find_by_mail(mail)

    if user.nil?
      #ユーザが存在しない(未確認ロジック)
      RAILS_DEFAULT_LOGGER.error "Failed to get user info via OAuth"
      flash[:notice] = "Authentication failed"
      redirect_to :action => :index
      return
    end

    #セッション情報をすべてリセット
    reset_session
    #最終更新日付更新
    user.update_attribute(:last_login_on, Time.now)
    #セッションに設定
    session[:user_id] = user.id
    #マイページにリダイレクト
    redirect_back_or_default :controller => 'my', :action => 'page'
  end

end

http://ドメイン/cybozulive
でサイボウズのログイン画面に遷移します。
f:id:mitsugi-bb:20110509073907p:image