Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Railsのカスタムセッションストア

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

 Railsのカスタムセッションストア

Avatar for Koichiro Okubo

Koichiro Okubo

February 23, 2018
Tweet

Other Decks in Programming

Transcript

  1. 2 ࣗݾ঺հ k-okubo ɾPerl Ͱ CGI ॻ͍ͨΓɺ ɾSIer ͰϝΠϯϑϨʔϜ৮ͬͨΓɺ ɾιγϟή։ൃͰ

    Java/C++ ͨ͠Γͯ͠ɺ ɾ1݄ʹαʔόαΠυΤϯδχΞͱͯ͠ ɹFablic ʹೖࣾ
  2. 8 ηογϣϯઃఆ (Ҡߦظؒத) GSJMKQ ओʹ1$޲͚αΠτ Set-Cookie: _fril_user_session=… ҉߸Խ͋Γ CBTFॻ͖ग़͠ XFCGSJMKQ

    औҾϖʔδ ϞόΠϧ͔Β΋ΞΫηε Set-Cookie: _fril_user_session=… ҉߸Խ͋Γ CBTFॻ͖ग़͠ ͨͩ͠ Cookie:_fril_web_session Λ౉͞Εͯ΋ಈ͘Α͏ʹ
  3. 10 w3BDL.JEEMFXBSFͱͯ͠ಈ࡞ wΞϓϦέʔγϣϯίʔυதͰTFTTJPOʹΞΫηε͕͋ΔͱMPBE@TFTTJPO͕ݺ͹ΕΔ wSFRVFTUͷ࠷ޙʹDPNNJU@TFTTJPO͢Δ class CookieStore < Rack::Session::Abstract::ID ... end

    class Rack::Session::Abstract::ID def call(env) context(env) end def context(env, app=@app) prepare_session(env) status, headers, body = app.call(env) commit_session(env, status, headers, body) end end CookieStore ͷ࣮૷Λ೷͘
  4. 13 def get_cookie(env) jar = cookie_jar(env) if jar.key?(@key) jar.encrypted[@key] else

    jar.signed[@old_key] end end ηογϣϯͷಡΈࠐΈ෦෼ BGUFS w ৽LFZͷ$PPLJF͕͋Ε͹ͦͬͪΛಡΈࠐΈ w ແ͚Ε͹چLFZͰಡΈࠐΈ
  5. 15 def set_cookie(env, session_id, cookie) cookie_jar(env)[@key] = cookie end ηογϣϯͷॻ͖ग़͠෦෼

    CFGPSF w DPNNJU@TFTTJPO͔Βݺ͹ΕΔ w ηογϣϯ৘ใΛγϦΞϥΠζͯ͠$PPLJFʹอଘ
  6. 16 def set_cookie(env, session_id, cookie) jar = cookie_jar(env) jar.encrypted[@key] =

    cookie if jar.key?(@old_key) jar.delete(@old_key) end end ηογϣϯͷॻ͖ग़͠෦෼ BGUFS w ৽LFZͰ$PPLJFʹॻ͖ग़͠ w چLFZͷ$PPLJF͸࡟আ
  7. 17 class CompatibleCookieStore < ActionDispatch::Session::CookieStore def initialize(app, options = {})

    super(app, options) @old_key = options[:old_session_key] end def get_cookie(env) jar = cookie_jar(env) if jar.key?(@key) jar.encrypted[@key] else jar.signed[@old_key] end end def set_cookie(env, session_id, cookie) jar = cookie_jar(env) jar.encrypted[@key] = cookie if jar.key?(@old_key) jar.delete(@old_key) end end def cookie_jar(env) request = ActionDispatch::Request.new(env) request.cookie_jar end end Rails.application.config.session_store :compatible_cookie_store, key: '_fril_user_session', expire_after: 1.month, domain: ‘.fril.jp’, old_session_key: '_fril_web_session' શମ૾