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

PassportではじめるOAuth2 #laravel_osaka

Hinaloe
October 29, 2016

PassportではじめるOAuth2 #laravel_osaka

Laravel 5.3 で公式パッケージとしてリリースされたPassport。その使い方や存在意義、特徴なんかをざっくり解説。
@Laravel.Osaka 2016 (2016.10.29.sat/MOTEX Inc.) http://php-jp.github.io/laravel-osaka-2016/

※リンクはPDFをDLしてからどうぞ

Hinaloe

October 29, 2016
Tweet

More Decks by Hinaloe

Other Decks in Technology

Transcript

  1. 1BTTQPSUͰ͸͡ΊΔ

    0"VUI
    !-BSBWFM0TBLB TBU.05&9*OD

    )JOBMPF

    View Slide

  2. "CPVUNF
    w ौ୩Ͱಇ͍ͯΔژ౎ͷֶੜ
    w ීஈ͸8PSE1SFTTͷਓ
    w -BSBWFMॳ৺ऀ

    !IOMF
    !IJOBMPF
    !IOBMPF
    CMPHIJOBMPFOFU
    )/)JOBMPF

    View Slide

  3. ͍͖ͳΓͰ͕͢ʜʜ
    20"VUIΛ࢖ͬͨ͜ͱ͕͋Δͬͯํ
    Ͳͷ͘Β͍ډ·͔͢ʁ

    ϓϩόΠμɺΫϥΠΞϯτ໰Θͣ


    View Slide

  4. ͍͖ͳΓͰ͕͢ʜʜ
    2Ͱ͸ɺͦͷதͰ΋

    ʮ0"VUIϓϩόΠμΛ࡞ͬͨ͜ͱ
    ͕͋Δʂʯͱ͍͏ํ

    View Slide

  5. 1BTTQPSU
    w -BSBWFM͔Βެࣜύοέʔδʹͳͬͨ
    0"VUI1SPWJEFS

    View Slide

  6. 8IBUT0"VUI

    View Slide

  7. 0"VUI
    w 0"VUI ΦʔΦʔε
    ͸ɺݖݶͷೝՄ
    BVUIPSJ[BUJPO
    Λߦ͏ͨΊͷΦʔϓϯελϯμʔ
    υͰ͋Δɻ 8JLJQFEJB


    View Slide

  8. 0"VUI
    w ֎෦αʔϏεʹ"1*΁ͷΞΫηεΛೝՄ͢Δ࣌౳ʹ

    ͔ͭΘΕΔ
    w (PPHMF
    w 'BDFCPPL
    w (JU)VC
    w %SPQCPY
    w 4MBDL
    w 5XJUUFS
    w BOEFUDʜʜ

    View Slide

  9. 0"VUI

    View Slide

  10. 0"VUI

    View Slide

  11. 0"VUI

    View Slide

  12. 0"VUI

    View Slide

  13. 0"VUI

    View Slide

  14. 0"VUI


    ͜Ε͸0"VUIB

    View Slide

  15. 0"VUI

    5XJUUFS͸"QQMJDBUJPOPOMZBVUIFOUJDBUJPOʹ࠾༻
    IUUQTEFWUXJUUFSDPNPBVUIBQQMJDBUJPOPOMZ

    View Slide

  16. 0"VUI͕Մೳʹ͢Δ͜ͱ
    w ΞϓϦέʔγϣϯ΁ͷΞΫηεΛೝՄ
    w ֎෦ΞϓϦέʔγϣϯ౳ͱͷ࿈ܞ
    w ϞόΠϧΞϓϦ౳͔ΒͷϩάΠϯɾೝূ
    w ϑϩϯτͱαʔόʔͷ෼཭

    View Slide

  17. 0"VUIͷϑϩʔ

    IUUQTXXXEJHJUBMPDFBODPNDPNNVOJUZUVUPSJBMTBOJOUSPEVDUJPOUPPBVUI

    View Slide

  18. ͪͳΈʹ0"VUI͸ʜʜ

    https://www-10.lotus.com/ldd/appdevwiki.nsf/xpAPIViewer.xsp?lookupName=API
    +Reference#action=openDocument&res_title=OAuth_1.0a_APIs_for_web_server_flow_sbt&content=apicontent

    View Slide

  19. 0"VUI͸

    ΫϥΠΞϯτ࣮૷ָ͕
    w )5514Λલఏͱͨ͠ೝূ

    ˠϦΫΤετ࣌ʹ)FBEFSΛ౤͛Δ͚ͩ

    ˠೝՄϓϩηε͕Θ͔Γ΍͍͢

    ˠෳࡶͳγάωνϟΛϦΫΤετຖʹܭࢉ͢Δ

    ɹඞཁ͕ͳ͍ɺCPEZ͸ͦͷ··౤͛ΒΕΔ

    ˠϑϩϯτ+4ʹ΋૊ΈࠐΈ΍͍͢ ᠘͸͋Δ


    View Slide

  20. -BSBWFMͱ0"VUI
    ·Ͱ

    View Slide

  21. -BSBWFMͱ0"VUI
    ͔Β

    a㊗ެࣜύοέʔδԽʂ

    View Slide

  22. ͪͳΈʹ-BSBWFMΞϓϦʹ

    ଞαʔϏεͷ440Λ͚ͭΔʹ͸ʜ
    ಉ͘͡ެࣜύοέʔδͷTPDJBMJUF͕࢖͑·͢
    ͓ͦΒ͘1BTTQPSUͷΫϥΠΞϯτʹ΋࢖༻Մ


    View Slide

  23. Feature of Passport

    View Slide

  24. 1BTTQPSU͸0"VUI1SPWJEFSϥΠϒϥϦͷ

    MFBHVFPBVUITFSWFS
    ͷ-BSBWFM޲͚ϥούʔΈ͍ͨͳ΋ͷ
    આ໌͕ࡶ

    View Slide

  25. -FBHVFPBVUITFSWFS͕

    σϑΥϧτͰαϙʔτͯ͠Δ[email protected]
    BVUIPSJ[[email protected]ˠೝՄίʔυϑϩʔ
    [email protected]
    ˠ͍ΘΏΔʮΞϓϦέʔγϣϯೝূʯ FY5XJUUFS

    JNQMJDJUˠDPEFͱࣅ͍ͯΔ͕ɺτʔΫϯΛίʔϧόοΫʹࡌͤΔ
    QBTTXPSEˠ*%ύεϫʔυʹΑΔೝՄ
    [email protected]ˠτʔΫϯߋ৽

    View Slide

  26. 1BTTQPSUͰ࢖͑Δ
    [email protected]
    BVUIPSJ[[email protected]ˠೝՄίʔυϑϩʔ
    [email protected]
    ˠ͍ΘΏΔʮΞϓϦέʔγϣϯೝূʯ FY5XJUUFS

    QBTTXPSEˠ*%ύεϫʔυʹΑΔೝՄ
    [email protected]ˠτʔΫϯߋ৽
    [email protected]ˠݸਓ༻τʔΫϯ

    View Slide

  27. BVUIPSJ[[email protected]
    w Α͋͘ΔɺʮϓϩόΠμଆ͕ϩάΠϯը໘ɺೝՄ
    ը໘Λఏڙ͢ΔʯελΠϧɻ
    w ηΩϡϦςΟ໘Ͱ΋͜Ε͕ਪ঑
    w σϑΥϧτͰ
    Ϣʔβʔͷ୭Ͱ΋ΫϥΠΞϯτΛ
    ൃߦՄೳ

    View Slide

  28. BVUIPSJ[[email protected]

    σϑΥϧτͷೝՄը໘
    είʔϓແ͠ είʔϓ͋Γ

    View Slide

  29. BVUIPSJ[[email protected]

    Ϣʔβʔ͕ಛఆΛϖʔδ PBVUIBVUIPSJ[F
    ʹΞΫηεͤ͞Δ
    ˠΫΤϦͰ[email protected] SFE[email protected] [email protected]
    TDPQF PQUJPO
    TUBUF PQUJPO
    Λ౉͢
    ˣ
    Ϣʔβʔ͕ϩάΠϯɺೝՄΛԡ͢
    ˣ
    ࢦఆͨ͠ϦμΠϨΫτઌ ݩΞϓϦͷ͸ͣ
    ʹɺΫΤϦʹDPEFΛ
    ͚ͭͯϦμΠϨΫτ͞ΕΔͷͰɺ
    PBVUIUPLFOΛୟ͍ͯτʔΫϯΛऔಘ

    View Slide

  30. BVUIPSJ[[email protected]

    τʔΫϯऔಘͷྫ

    View Slide

  31. QBTTXPSE
    w Ϣʔβʔ໊ͱύεϫʔυΛ௚઀ࢦఆͯ͠τʔΫϯ
    Λऔಘ͢Δ
    w ը໘ભҠ͕ෆཁɺϒϥ΢β͕ͳͯ͘΋ೝՄՄೳ
    w TDPQF<>͕࢖༻Մೳ
    w جຊతʹTUQBSUZͷΈ͕࢖༻Մ
    w ۃྗ࢖༻͢΂͖Ͱ͸ͳ͍

    View Slide

  32. QBTTXPSE

    1045ͷྫ
    Ϩεϙϯε͸ಉ༷ͳͷͰলུ

    View Slide

  33. QBTTXPSEೝূ༻
    ΫϥΠΞϯτΛൃߦ͢Δʹ͸ʁ
    BSUJTBOQBTTQPSUJOTUBMMͷࡍʹൃߦ͞ΕΔ
    BSUJTBOQBTTQPSUDMJFOUŠQBTTXPSEͰൃߦ
    ˞QBTTXPSEઐ༻ΫϥΠΞϯτͱͳΓɺ

    DPEFೝূʹ͸ར༻Ͱ͖ͳ͍

    View Slide

  34. [email protected]
    w 0"VUIͷτʔΫϯ͸ηΩϡϦςΟͷͨΊɺظݶ
    ୹Ί͕ਪ঑
    w ୅ΘΓʹ[email protected]ͱ͍͏ʮτʔΫϯΛߋ
    ৽͢ΔͨΊͷτʔΫϯʯ͕ଘࡏ
    w ͜ΕΛར༻ͯ͠ɺ࣮࣭൒߃ٱԽ͸Մೳ ܧ͗଍͠


    View Slide

  35. View Slide

  36. [email protected]
    w ݸਓ༻τʔΫϯ
    w ظݶ͕࣮࣭Ӭٱ ߋ৽ෆཁ

    w ։ൃ༻τʔΫϯ΍ϫϯϥΠφʔ౳ʹ΋ศར
    w ΫϥΠΞϯτͷ֓೦͕ଘࡏ͠ͳ͍ಛघͳଘࡏ
    w ྫ͑͹-*/&/PUJGZ΋͜ΕΛ࢖ͬͯ൥Θ͍͠ೝূ
    ෆཁͷίʔυ͕ॻ͚ΔɺΈ͍ͨͳɻ

    View Slide

  37. [email protected]"1*
    w ૊ΈࠐΈͷ1"5औಘ"1*ɿ

    PBVUIQFSTPOBMBDDFTT
    UPLFOT
    w ϦΫΤετྫɿ

    \OBNFτʔΫϯ໊
    TDPQFT<> FSSPST<>^
    w ཁ8FCϩάΠϯ DTSGϔομʔ

    View Slide

  38. [email protected]
    w ΄͔ɺVTFSDSFBUF5PLFO
    Ͱ΋೚ҙʹτʔΫ
    ϯ͕ൃߦͰ͖Δɻ

    View Slide

  39. [email protected]
    w ଞͱ͸ҧ͍ɺʮϢʔβʔʯͰ͸ͳ͘ʮΫϥΠΞ
    ϯτʯ ΞϓϦέʔγϣϯ
    Λೝূ͢Δ
    w Ұൠެ։ʹ͸͠ͳ͍͕ʮΞϓϦ͔ΒͳΒݟΕ
    ΔʯɺೝূࡁΈͳΒ3BUF-JNJU௿ݮɺͳΜ͔ʹ࢖ͬ
    ͨΓ 5XJUUFS

    w جຊతʹύϒϦοΫͳϦιʔεʹΞΫηε͢Δͨ
    Ίͷ΋ͷ

    View Slide

  40. View Slide

  41. [email protected]
    ͳ͓ɺ[email protected]ͷΈڐՄ͢Δ

    ϦιʔεΛͭ͘Δʹ͸ɺ

    $IFDL$MJFOU$SFEFOUJBMTϛυϧ΢ΣΞΛ

    ࢖͑͹ྑ͍ɻ

    View Slide

  42. *OTUBMMBUJPO

    View Slide

  43. 1BTTQPSUͷΠϯετʔϧ
    w આ໌͢Δ΄Ͳͷ΋ͷͰ΋ͳ͍ͷͰϚχϡΞϧ͔

    ࢲͷϒϩάΛಡΜͰ͍ͩ͘͞ʂʢ
    w IUUQTCMPHIJOBMPFOFUUSZ
    QBTTQPSUMBSBWFM

    View Slide

  44. Πϯετʔϧ͢Δͱʜʜ
    w ઌఔ঺հ֤ͨ͠छೝՄํ๏͕σϑΥϧτͰ༗ޮ
    ʹ
    w τʔΫϯ؅ཧपΓͷϢʔβʔ༻"1*͕༗ޮʹ

    XFC"VUIɺཁDTSG


    View Slide

  45. ೝূ෇ϦΫΤετʜʜ
    w ֤ೝՄํ๏Ͱऔಘͨ͠#FBSFSτʔΫϯΛIFBEFS
    ʹηοτͯ͠ϦΫΤετ͢Δ͚ͩʂ
    w "VUIPSJ[BUJPO#FBSFSFZ+F9"J0J+,ʜʜ

    View Slide

  46. ࿈ܞɾΫϥΠΞϯτ؅ཧ

    XJUI7VFKT
    w τʔΫϯ΍ΫϥΠΞϯτ
    ͷൃߦ΍؅ཧΛߦ͏ͨΊ
    ͷ7VFίϯϙʔωϯτ͕
    ෇ଐ

    View Slide

  47. τʔΫϯͷਖ਼ମ
    w 1BTTQPSUͷ#FBSFSτʔΫϯͷ࣮ମ͸+85
    +40/8FC5PLFO

    w Ͱ۠੾ΒΕͨCBTFFODPEFE+40/
    w ͭ·Γ

    UPLFOTQMJU
    TMJDF

    NBQ BUPC
    NBQ +40/QBSTF

    Ͱʜʜʜʜ

    ͱ͜ΖͰ͍͖ͳΓ͚ͩͲ

    View Slide

  48. ΧελϚΠζฤ

    View Slide

  49. 1BTTQPSUͷઃఆ
    w -BSBWFMͷύοέʔδͬͯେ఍DPOpHͰઃఆͰ͖
    ΔΑ͏ʹͳͬͯΔ͔ΒͦΕฤू͢Ε͹͍͍ΑͶɺ
    ͱ͍͏ߟ͑͸؁͍ɻ
    w 1BTTQPSUʹ͸DPOpHϑΝΠϧͳΜͯ΋ͷ͸

    ͳ͔ͬͨʜʜ

    View Slide

  50. -FWFM
    ʮݟͨ໨ʯͷΧελϚΠζ

    View Slide

  51. -FWFMݟͨ໨ͷΧελϚΠζ
    w ೝՄը໘ͷݟͨ໨΍จݴ͸ɺ͍ͭ΋Ͳ͓Γ

    BSUJTBOWFOEPSQVCMJTIͯ͠resources/views/
    vendor/passport/authorize.blade.phpΛ͍͍ײ
    ͡ʹͯ͠΍Ε͹͍͍ɻ
    w ͓ͦΒ͘ଟݴޠରԠ΋Մ

    View Slide

  52. -FWFM
    τʔΫϯ؅ཧϖʔδͷ࡞੒

    View Slide

  53. -FWFMτʔΫϯ؅ཧը໘
    w طʹ7VFΛ࢖͍ͬͯΔϓϩδΣΫτͳΒਂ͘ߟ͑
    ͣʹ
    w طʹ࢖͍ͬͯͳͯ͘΋σϑΥϧτͷBQQKT͕͋Ε
    ͹
    w ˠOQNSVOQSPEͰ7VFʹରԠͰ͖ΔͷͰʜʜ

    View Slide

  54. -FWFMτʔΫϯ؅ཧը໘
    SFTPVSDFTBTTFUTKTBQQKTʹ

    Vue.component('authorized-clients', require('./
    components/passport/AuthorizedClients.vue'));
    Vue.component('clients', require('./components/
    passport/Clients.vue'));
    Vue.component('personal-tokens', require('./
    components/passport/PersonalAccessTokens.vue'));
    Λॻ͖଍ͯ͠΍Γɺ೚ҙͷWJFXͰBVUIPSJ[FEDMJFOUTDMJFOUT
    QFSTPOBMUPLFOTͷΑ͏ʹॻ͍ͯ΍Ε͹͓̺
    ඞཁʹԠͯ͡ίϯϙʔωϯτ΋ฤू͠·͠ΐ͏

    View Slide

  55. -FWFM
    w 1BTTQPSUͷTUBUJDNFUIPEͰઃఆ

    View Slide

  56. -FWFMTUBUJDNFUIPE
    w 1BTTQPSUSPVUF
    লུͤͣʹॻ͘ͱʜʜˠ
    w 1BTTQPSUSPVUF GVODUJPO 3PVUF3FHJTUSBSSPVUFS
    \

    SPVUFSGPS"VUIPSJ[BUJPO

    SPVUFSGPS"DDFTT5PLFOT

    SPVUFSGPS5SBOTJFOU5PLFOT

    SPVUFSGPS$MJFOUT

    SPVUFSGPS1FSTPOBM"DDFTT5PLFOT

    ^


    View Slide

  57. -FWFMTUBUJDNFUIPE
    w 1BTTQPSUQSVOF3FWPLFE5PLFOT
    SFWPLF࣌ʹ%#͔Βফ͢
    w 1BTTQPSUQFSTPOBM"DDFTT$MJFOU DMJFOU*E

    w 1BTTQPSUUPLFOT$BO BSSBZTDPQFT

    w 1BTTQPSUUPLFOT&YQJSF*O %BUF5JNF*OUFSGBDFEBUFOVMM

    w 1BTTQPSUSFGSFTI5PLFOT&YQJSF*O %BUF5JNF*OUFSGBDFEBUFOVMM

    w 1BTTQPSUMPBE,FZT'SPN QBUI

    w 1BTTQPSULFZ1BUI pMF

    w 1BTTQPSUJHOPSF.JHSBUJPOT


    View Slide

  58. NFNPTDPQF

    Passport::tokensCan([

    'read' => '౤ߘɺϓϩϑΟʔϧͷදࣔ',

    'write' => '౤ߘͷ࡞੒ɺฤू΍ϓϩϑΟʔϧͷมߋ',

    'message' => 'DirectMessageͷදࣔ',

    ]);

    View Slide

  59. NFNPTDPQF

    // kernel

    'scopes' => \Laravel\Passport\Http\Middleware\CheckScopes::class,

    'scope' => \Laravel\Passport\Http\Middleware\CheckForAnyScope::class,

    // router
    Route::get('/orders', function () {

    // ΞΫηετʔΫϯ͸"check-status"ͱ"place-orders"ɺ྆είʔϓΛ͍࣋ͬͯΔ

    })->middleware('scopes:check-status,place-orders');

    Route::get('/orders', function () {

    // ΞΫηετʔΫϯ͸ɺ"check-status"͔"place-orders"ɺͲͪΒ͔ͷείʔϓΛ͍࣋ͬͯΔ

    })->middleware('scope:check-status,place-orders');

    Route::get('/orders', function (Request $request) {

    if ($request->user()->tokenCan('place-orders')) {

    //

    }

    });


    View Slide

  60. -FWFM
    w 4FSWJDF1SPWJEFSͷΦʔόʔϥΠυ

    View Slide

  61. -FWFMPWFSSJEF
    w 1BTTQPSU4FSWJDF1SPWJEFSΛܧঝͨ͠ΫϥεΛ༻
    ҙ͠ɺڍಈΛมߋ͍ͨ͠෦෼ΛΦʔόʔϥΠυ
    w 1BTTQPSUʹݶΒͣ-BSBWFMશൠͰΑ͋͘Δ࡞ۀ
    w ͱΓ͋͑ͣίʔυΛಡΉ ͋·Γ΍Γͨ͘ͳ͍

    w ύοέʔδͷΞοϓσʔτ಺༰ʹ஫ҙ

    View Slide

  62. -FWFMPWFSSJEF
    ͨͱ͑͹ʜʜ
    class BearerTokenResponse extends \League\OAuth2\Server
    \ResponseTypes\BearerTokenResponse

    {

    protected function
    getExtraParams(AccessTokenEntityInterface $accessToken)

    {

    return ['scope'=>$accessToken->getScopes()];

    }

    }


    View Slide

  63. ͳͥ࢖͏ͷ͔ɺฤ

    View Slide

  64. 8IZ1BTTQPSU
    w ͜Μͳঢ়گʹ͏Ε͍͠
    w ද͸41" ཪ͸"1* υϝΠϯ෼཭
    ˡ࣮ࡍʹӡ༻த
    w ϞόΠϧΞϓϦͷόοΫΤϯυͱͯ͠ ΋
    "1*Λ࢖͏ɺ͋
    Δ͍͸ͦΕΛݕ౼͍ͯ͠Δ
    w αʔυύʔςΟ͕ΫϥΠΞϯτΞϓϦΛ࡞ΕΔΑ͏ʹͨ͠
    ͍
    w 440͕ग़དྷΔΑ͏ʹ͍ͨ͠

    View Slide

  65. 8IZ1BTTQPSU
    w ͜Μͳ࢖͍ํ΋ʜʜ
    w +85ͷಛੑΛར༻ͯ͠ɺ"1*(BUFXBZʹߏஙͨ͠
    "1*ʹτʔΫϯͷ࢖͍ճ͠
    w ˠ࠷ѱ%#͕ࢮΜͰ͍ͯ΋ࣦޮݕূҎ֎Մೳ
    aϚΠΫϩαʔϏε

    View Slide

  66. ௕͘ͳ͚ͬͨͲ
    ͪΐͬͱ͚ͩσϞ

    View Slide

  67. Ҏ্
    ͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ

    View Slide

  68. ࢀߟ
    w -BSBWFMͰ1BTTQPSUΛࢼ͢cͽΜ͍͘Ζʹ͖ͬ
    w "1*ೝূ 1BTTQPSU
    -BSBWFM
    w 0"VUI4FSWFS1)1
    w -BSBWFMͷ1BTTQPSUͰར༻Ͱ͖Δೝূํ๏Λ੍ݶ
    ͢Δ2JJUB

    View Slide