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

JSON Web Token Authentication 
for Mobile Application

Harukasan
October 08, 2014

JSON Web Token Authentication 
for Mobile Application

JSON Web Token Authentication 
for Mobile Application

社内説明用資料

Harukasan

October 08, 2014
Tweet

More Decks by Harukasan

Other Decks in Technology

Transcript

  1. Agenda • JSON Web Token (JWT) • OAuth 2.0 JWT

    Bearer Token Profile • OAuth 2.0 Client Authentication for Mobile Application • Certificate Pinning
  2. JSON Web Token (JWT) ! • HTTP Header΍ΫΤϦύϥϝʔλͷΑ͏ͳαΠζʹ੍ݶ͕͋ΔྖҬͰ࢖༻ ͞ΕΔ͜ͱΛҙਤͨ͠ίϯύΫτͳΫϨʔϜදݱϑΥʔϚοτ •

    JSONΦϒδΣΫτ(จࣈྻ)ʹΤϯίʔυ͢Δ • ӳ୯ޠͷ”jot”ͱಉ͡ൃԻ͕ਪ঑͞Ε͍ͯΔ • Google, Salesforce, Yahoo JapanͰ࠾༻
  3. JWE / JWS • ϖΠϩʔυͱͯ͠JWE/JWSͷ2͕ͭ͋ΓɺͦΕͧΕ࢓༷ͱͯ͠෼ׂ͞Εͨ • JSON Web Encryption (JWE):

    ίϯςϯτͷ҉߸ԽΛߦ͏ • JSON Web Segnature (JWS): ίϯςϯτͷॺ໊Λߦ͏ ! • ࢖༻ࣄྫ͕ଟ͍ͷ͸JWS • OAuth 2.0 / OpenID Connect
  4. The structure of JWT(JWS) • ϔομ+ϖΠϩʔυ+γάωνϟͷ3ͭͰߏ੒ • BASE64URLΤϯίʔυͨ͠จࣈྻΛ”.”Ͱ࿈݁ͨ͠จࣈྻͱͯ͠දݱ͞ΕΔ { "typ":"JWT",

    "alg":"HS256" } { "iss":"JWT", "sub":"HS256", "exp":1412735596,... } BASE64URL(HMACSHA256(
 BASE64URL(header) +
 "." + BASE64URL(payload),
 "shared_key")) Header Payload Signature ewogICJ0eXAiOiJKV1QiLAogICJh bGciOiJIUzI1NiIKfQo ewogICJpc3MiOiJKV1QiLAogICJz dWIiOiJIUzI1NiIsCiAgImV4cCI6 MTQxMjczNTU5NiwuLi4KfQo NWYzOGY0NmYwYjlkYWY3Mjc0OGI3 MWVlNzRhMDRlMGM2ZDlkNzVmNmZk OWJmMGM3ZTViYmE3MjU3OTY4MThj ZQo BASE64 URL ENCODED ewogICJ0eXAiOiJKV1QiLAogICJhbGciO iJIUzI1NiIKfQo.ewogICJpc3MiOiJKV1 QiLAogICJzdWIiOiJIUzI1NiIsCiAgImV 4cCI6MTQxMjczNTU5NiwuLi4KfQo.NWYz OGY0NmYwYjlkYWY3Mjc0OGI3MWVlNzRhM DRlMGM2ZDlkNzVmNmZkOWJmMGM3ZTViYm E3MjU3OTY4MThjZQo concat JSON Web Token
  5. Registered claims {"iss":"issuer",
 "sub":"subject", "aud":"audience", "exp":1412745300, "nbf":1412744700, "iat":1412745000,
 "jti":"8046…0c",
 "typ":"type"}

    issuer: ΫϨʔϜͷൃߦऀ subject: ର৅ audience: ར༻ऀ expiration: ༗ޮظݶ not before: ༗ޮʹͳΔ࣌ࠁ issued at: ൃߦ࣌ࠁ JWT ID: Ұҙͳࣝผࢠ(ID) type: ίϯςϯτλΠϓ
  6. Signature algorithm alg Digital Signature / MAC Algorithm Requirements HS256

    HMAC using SHA-256 Required HS384 HMAC using SHA-384 Optional HS512 HMAC using SHA-512 Optional RS256 RSASSA-PKCS-v1.5 using SHA-256 Recommended RS384 RSASSA-PKCS-v1.5 using SHA-384 Optional RS512 RSASSA-PKCS-v1.5 using SHA-512 Optional ES256 ECDSA using P-256 and SHA-256 Recommended+ ES512 ECDSA using P-384 and SHA-384 Optional PS256 RSASSA-PSS using SHA-256 and MGF1 with SHA-256 Optional PS384 RSASSA-PSS using SHA-384 and MGF1 with SHA-384 Optional PS512 RSASSA-PSS using SHA-512 and MGF1 with SHA-512 Optional
  7. OAuth 2.0 JWT Bearer token profile • JWTʹΑΓΞΫηετʔΫϯΛཁٻ͢ΔOAuth2.0ͷ֦ு࢓༷ • JWTΛ༻͍ͯGrant

    AuthorizationΛߦ͏ • JWTΛ༻͍ͯClient AuthenticationΛߦ͏ • Client Authentication͸OAuth 2.0 Assertionsͱͯ͠ඪ४Խ͕ਐΜͰ͍ Δ֦ு࢓༷ • JWSʹΑΓॺ໊Λߦ͏ͷͰൿີ伴ɺ·ͨ͸ڞ༗伴͕࿙Ӯ͠ͳ͍ݶΓ
 τʔΫϯͷվ͟Μ͕೉͍͠ • IDΛ֬ೝ͢Δ͜ͱͰϦϓϨΠ߈ܸΛ๷͙͜ͱ͕ग़དྷΔ
  8. JWT Bearer token flow Client Authorization Server private key public

    key grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer
 assertion=(JWT token) access_token=(ACCESS TOKEN) … • JWTτʔΫϯΛ࢖༻ͯ͠ΞΫηετʔΫϯΛऔಘ͢Δ
  9. JWT for grant authorisation • ϖΠϩʔυʹ͸ҎԼͷ஋Λࢦఆ͢Δ {"iss":"issuer",
 "sub":"subject", "aud":"audience", "exp":1412745300,

    "nbf":1412744700, "iat":1412745000,
 "jti":"8046…0c"} ΫϨʔϜͷൃߦऀͷUIDΛؚΊΔ ର৅(Ϣʔβʔ໊΍ϝʔϧΞυϨε) [REQUIRED] ड৴ऀ(ೝূαʔό) ༗ޮظݶ [REQUIRED] ༗ޮʹͳΔ࣌ࠁ [OPTIONAL] ൃߦ࣌ࠁ [OPTIONAL] Ұҙͳࣝผࢠ [REQUIRED]
  10. Grant authentication using JWT • JWTΛassertionύϥϝʔλʹࢦఆͯ͠ϦΫΤετ͢Δ • grant_type͸"urn:ietf:params:oauth:client-assertion-type:jwt-bearer" • αʔό͸ॺ໊Λݕূ͠ݖݶͷaccess_tokenΛฦ͢

    POST /token/oauth2 HTTP/1.1 Host: www.example.com Content-Type: application/x-www-form-urlencoded ! grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer &assertion=eyJhbGciOiJFUzI1NiJ9.eyJpc3Mi[…snip…].J9[…snip…]wP &scope=scope
  11. Client authentication in OAuth 2.0 Asserttion Framework for OAuth 2.0

    Client Authentication and Authorization Grants
 http://tools.ietf.org/html/draft-ietf-oauth-assertions-16 • 3rd party applicationͳͲͷΫϥΠΞϯτΛೝূ͢ΔͨΊͷ࢓༷ Relying
 Party Client Token Service Relying
 Party Client Third Party Created Assertion Self-issued Assertion Assertion Assertion
  12. Issuing client assertion • ҎԼͷ಺༰ΛؚΊΔ • Client assertionͷൃߦऀ͸ୈ3ऀͰ΋ΫϥΠΞϯτࣗ਎Ͱ΋ྑ͍ {"iss":"issuer",
 "sub":"subject",

    "aud":"audience", "exp":1412745300, "nbf":1412744700, "iat":1412745000,
 "jti":"8046…0c" } ΫϨʔϜͷൃߦऀͷUIDΛؚΊΔ client_id ड৴ऀ(ೝূαʔό) ༗ޮظݶ [REQUIRED] ༗ޮʹͳΔ࣌ࠁ [OPTIONAL] ൃߦ࣌ࠁ [OPTIONAL] Ұҙͳࣝผࢠ [REQUIRED]
  13. Client authentication using assertion • ϦΫΤετΛߦ͏ࡍʹClient AssertionΛ෇Ճ͢Δ • client_assertion_type͸
 urn:ietf:params:oauth:client-assertion-type:jwt-bearer

    POST /token/oauth2 HTTP/1.1 Host: www.example.com Content-Type: application/x-www-form-urlencoded ! grant_type=authorization_code
 &code=37ac5695c507be6a15a093268fb7c592 &client_assertion_type=urn%3Aietf%3Aparams%3Aoauth
 %3Aclient-assertion-type%3Ajwt-bearer
 &client_assertion=PHNhbW[…snip…]ZT
  14. Client Authentication in Mobile App Client Server private key public

    key grant_type=password
 &username=(user name)
 &password=(password) &client_assertion_type=urn%3Aietf%3Aparams
 %3Aoauth%3Aclient-assertion-type%3Ajwt-bearer
 &client_assertion=(client assertion) access_token=(ACCESS TOKEN) … 1. Issue Client Assertion 2. Password authorization with client assertion
  15. Issuing Client Assertion • Client Assertion͸ϞόΠϧΞϓϦέʔγϣϯࣗ਎͕ൃߦ͢Δ • ΞϓϦέʔγϣϯʹ͸ॺ໊ͷͨΊͷൿີ伴ΛຒΊࠐΜͰ͓͘ { "iss":"(Mobile

    App GUID)",
 "sub":"(client_id)", "aud":"(authorization server URI)", "exp":1412745300, "nbf":1412744700, "iat":1412745000,
 "jti":"(GUID)" } Client Assertion Payload
  16. Password Authorization
 with Client Authentication • Ϣʔβͷݖݶೝূʹ͸ύεϫʔυೝূΛ࢖༻͢Δ • ύεϫʔυೝূΛߦ͏ࡍʹclient assertionΛ෇Ճͯ͠ϦΫΤετ͢Δ

    POST /token/oauth2 HTTP/1.1 Host: www.example.com Content-Type: application/x-www-form-urlencoded ! grant_type=password
 &username=username
 &password=password &client_assertion_type=urn%3Aietf%3Aparams%3Aoauth
 %3Aclient-assertion-type%3Ajwt-bearer
 &client_assertion=PHNhbW[…snip…]ZT
  17. Sniff the access token from the channels • ΫϥΠΞϯτʹෆਖ਼ͳূ໌ॻΛΠϯετʔϧ͢Ε͹ୈ3ऀ͕TLSূ໌ॻΛ
 ͢Γସ͑ͯ௨৴͢Δ͜ͱ͕ग़དྷΔ

    Client MIM
 PROXY HTTPS SELF SIGNED CA CERTIFICATE Man In the Middle ATTACK SELF SIGNED CERTIFICATE HTTPS Server
  18. MITM attacks to sniff the access token • ௨৴಺༰͸Ϣʔβʔʹ༰қʹ࿐ఄ͢Δ •

    ΦʔϓϯιʔεͷϓϩΩγ΋ͨ͘͞Μ͋ͬͯศར • ωΠςΟϒΞϓϦέʔγϣϯʹ͓͍ͯ௨৴࿏ͷൿಗੑ͸ΫϥΠΞϯτ
 ར༻ऀͷखʹҕͶΒΕ͍ͯΔ • ΫϥΠΞϯτར༻ऀ͕ނҙʹߦ͑͹؆୯ʹதؒऀ߈ܸ͕Մೳ • ͦ΋ͦ΋ୈ3ऀͰ͸ͳ͍
  19. Pinning certificate and public key • TLSূ໌ॻ͕ਖ਼͍͠΋ͷ͔Ͳ͏͔ΫϥΠΞϯτͰݕূΛߦ͏ • αʔόূ໌ॻ·ͨ͸CAূ໌ॻͷݕূΛߦ͏ •

    ݕূʹ͸ূ໌ॻɺ·ͨ͸ެ։伴ͷϑΟϯΨʔϓϦϯτΛ࢖͏ • ެ։伴ͷϑΟϯΨʔϓϦϯτΛ࢖͑͹ূ໌ॻΛൃߦ͠ͳ͓ͯ͠΋ϑΟϯ ΨʔϓϦϯτΛߋ৽͢Δඞཁ͕ͳ͍ • iOSͰ͸ެ։伴ͷϑΟϯΨʔϓϦϯτΛܭࢉ͢Δͷ͕೉͍͠Έ͍ͨʁ
  20. Obtaining list of fingerprints • ϑΟϯΨʔϓϦϯτͷϦετ͸தؒऀ߈ܸʹΑΓվ͟Μ͞ΕΔՄೳੑ͕͋Δ • JWTΛ༻͍ͯϑΟϯΨʔϓϦϯτ͕มߋ͞ΕΔ͜ͱΛ๷͙ { "iss":"(authorization

    server URI)", "aud":"(client app name)", "exp":1412745300, "certs": ["624C18A73174F8E7988CB636CED6334AD8FEBC72"],
 "jti":"(GUID)" }
  21. Conclusion • OAuth 2.0ͰJWTʹΑΔGrant AuthorizationΛߦ͏ʹ͸
 OAuth 2.0 JWT Bearer token

    profileΛ࢖͏ • Client-side ApplicationͰ͸ݖݶͷڐՄҎ֎ʹΫϥΠΞϯτೝূ͕
 ඞཁʹͳΔ • TLSΑΓ্ͷϨΠϠʔͰͷೝূॲཧ͕ඞཁʹͳΔ • JSONͰͷ҉߸Խ/ॺ໊͸JWTΛ࢖͏ͱศར
  22. References • OAuth Documentation
 http://oauth.net/documentation/ • [RFC6749] The OAuth 2.0

    Authorization Framework
 http://tools.ietf.org/html/rfc6749 • JSON Web Token
 https://tools.ietf.org/html/draft-ietf-oauth-json-web-token-27 • JSON Web Signature (JWS)
 http://tools.ietf.org/html/draft-ietf-jose-json-web-signature-33 • JSON Web Encryption (JWE)
 http://tools.ietf.org/html/draft-ietf-jose-json-web-encryption-32