authentication and authorization in modern javascript web...

20
Authentication and authorization in modern JavaScript web applications How hard can it be? Brock Allen [email protected] http://brockallen.com @BrockLAllen

Upload: others

Post on 16-Oct-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Authentication and authorization in modern JavaScript web applicationssddconf.com/brands/sdd/library/Authentication_and... · 2016. 5. 16. · • Authentication for JavaScript web

Authenticationandauthorizationinmodern

JavaScriptwebapplicationsHowhardcanitbe?

[email protected]://brockallen.com

@BrockLAllen

Page 2: Authentication and authorization in modern JavaScript web applicationssddconf.com/brands/sdd/library/Authentication_and... · 2016. 5. 16. · • Authentication for JavaScript web

Outline

• ConstraintswithJavaScriptwebapplications• Affectshowweimplementsecurity

• OpenID Connect• AuthenticationforJavaScriptwebapplications• AuthenticationandauthorizationtoAPIs

• Applicationconsiderations• Tokenvalidation• Tokenmanagement

Page 3: Authentication and authorization in modern JavaScript web applicationssddconf.com/brands/sdd/library/Authentication_and... · 2016. 5. 16. · • Authentication for JavaScript web

Modern/PureJavaScriptapps

• Client• Browser-based• EntirelyJavaScript(SPA)• Dynamicrenderingallclientside

• Sever• Thinserver• Staticcontent(HTML,JS,CSS,etc.)• Ajaxendpoints(HTTPAPIs)

Page 4: Authentication and authorization in modern JavaScript web applicationssddconf.com/brands/sdd/library/Authentication_and... · 2016. 5. 16. · • Authentication for JavaScript web

SecuringmodernJavaScriptapps

• Client• Whoistheuser

• Server• Whoisthecaller

• User• Client

?

?

?

Page 5: Authentication and authorization in modern JavaScript web applicationssddconf.com/brands/sdd/library/Authentication_and... · 2016. 5. 16. · • Authentication for JavaScript web

Nomorecookiesforsecurity

• Cookiesarethetypicalapproachforserver-sideapplications• ButnotappropriateformodernJavaScriptapps

• Modernappsdon'thave/useserver-sideHTMLframework• SPAs(ormobileapps)aredoingtheUIclient-side

• APIscan'tusecookies• APImightbecross-domain• Cookiesdon'tmakesensefornon-browserclients• Cross-siterequestforgery(XSRF)securityissues

Page 6: Authentication and authorization in modern JavaScript web applicationssddconf.com/brands/sdd/library/Authentication_and... · 2016. 5. 16. · • Authentication for JavaScript web

OpenID Connectforsecurity

• OpenID Connect(OIDC)modernsecurityprotocol• Designedformodernapplicationtypes(client-side,server-side,andmobile)

• Allowsforauthenticationtoclientapplication• Withid_token

• AllowsforsecuringserverAPIs• Withaccess_token

Page 7: Authentication and authorization in modern JavaScript web applicationssddconf.com/brands/sdd/library/Authentication_and... · 2016. 5. 16. · • Authentication for JavaScript web

AuthenticationinJS-basedapps

• OpenId Provider(OP)• Issuestokens

• 1)ClientmakesrequesttoOP• Userauthenticates• Userconsents(optional)

• 2)OPreturnstoclient• Acceptidtoken• Clientvalidatesidtoken

bob

secret

id_token

Page 8: Authentication and authorization in modern JavaScript web applicationssddconf.com/brands/sdd/library/Authentication_and... · 2016. 5. 16. · • Authentication for JavaScript web

Idtokens

• FormatisJSONwebtoken(JWT)eyJhbGciOiJub25lIn0.eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMD.4MTkzODAsDQogImh0dHA6Ly9leGFt

Header Claims Signature{

"typ": "JWT","alg": "RS256","x5t": "mj399j…"

}

{"iss": "https://idsrv3","exp": 1340819380,"aud": "app1","nonce": "289347898934823",

"sub": "182jmm199","email": "[email protected]","email_verified": true,"amr": "password","auth_time": 12340819300

}

Page 9: Authentication and authorization in modern JavaScript web applicationssddconf.com/brands/sdd/library/Authentication_and... · 2016. 5. 16. · • Authentication for JavaScript web

Validatingidtokens

• Stepstovalidate:1. Verifystate issameassentinrequest(preventsXSRF/replay)2. Base64Urldecodeid_token andparseintoJSON(formattingstep)3. Verifynonce issameassentinrequest(preventsXSRF/replay)4. Validatesignature ontoken(establishestrust[requirescrypto])5. Validateiss sameasissuerofOIDCOP(establishestrust)6. Validateaud sameasthisclient'sidentifier(preventsprivilegeescalation)7. Validateexp isstillvalid(preventsstaletokens)

Page 10: Authentication and authorization in modern JavaScript web applicationssddconf.com/brands/sdd/library/Authentication_and... · 2016. 5. 16. · • Authentication for JavaScript web

OidcClient

• JavaScripthelperclassthatimplementsOIDCprotocol• Includesid_token validation

• Includingcryptoimplementation• Heavyuseofpromises

• http://github.com/IdentityModel/oidc-client-js• Alsoavailablevianpm

Page 11: Authentication and authorization in modern JavaScript web applicationssddconf.com/brands/sdd/library/Authentication_and... · 2016. 5. 16. · • Authentication for JavaScript web

Moreidentitydata

• Mightneedmorethansub (subject)claim• scope usedtoaskformoreidentitydata

Page 12: Authentication and authorization in modern JavaScript web applicationssddconf.com/brands/sdd/library/Authentication_and... · 2016. 5. 16. · • Authentication for JavaScript web

Moreidentitydatawithuserprofile

• Idtokenmightbecometoolarge• NeedstofitintoURL

• OIDCdefinesuserinfoendpoint• Ajaxcalltoloaduserprofile• RequiresauthorizationwithanaccesstokenobtainedinOIDCrequest

Page 13: Authentication and authorization in modern JavaScript web applicationssddconf.com/brands/sdd/library/Authentication_and... · 2016. 5. 16. · • Authentication for JavaScript web

Requestingaccesstoken

• Add"token"toresponse_typeparametertoauthorizationendpoint• Morevalidationrequired(sameasbefore,plus):• Hashaccesstokenandcomparelefthalftoat_hash inidtoken(ensuresidtokenispairedwithaccesstoken)

id_tokenaccess_token

access_token

userprofile

id_tokentoken

Page 14: Authentication and authorization in modern JavaScript web applicationssddconf.com/brands/sdd/library/Authentication_and... · 2016. 5. 16. · • Authentication for JavaScript web

Usingaccesstokentocalluserprofile

• AccesstokenpassedasAuthorizationHTTPrequestheader• ResponseisJSONofuserprofilebaseduponrequestedscopes

var xhr = new XMLHttpRequest();xhr.onload = function () {

var user_profile = JSON.parse(xhr.response);}

xhr.open("GET", user_profile_endpoint);xhr.setRequestHeader("Authorization", "Bearer " + access_token);xhr.send();

Page 15: Authentication and authorization in modern JavaScript web applicationssddconf.com/brands/sdd/library/Authentication_and... · 2016. 5. 16. · • Authentication for JavaScript web

CallingotherwebAPIs

• APIsuseaccesstokenfromsameOIDCOP• Justneedtorequestmorescopes

access_token

access_token

JSON

scope:api1

Page 16: Authentication and authorization in modern JavaScript web applicationssddconf.com/brands/sdd/library/Authentication_and... · 2016. 5. 16. · • Authentication for JavaScript web

Logout

• Throwawaytokensinclient• SigningoutofOIDCOP• MustmakerequesttoOP

• Postlogoutredirect• MustpassredirectURLaspost_logout_redirect_uri• Mustpassoriginalidtokenasid_token_hint

Page 17: Authentication and authorization in modern JavaScript web applicationssddconf.com/brands/sdd/library/Authentication_and... · 2016. 5. 16. · • Authentication for JavaScript web

Tokenmanagement

• Tokenstorage• localStorage• sessionStorage• indexedDb

• Tokenexpiration• Accesstokensexpire(1h,10h,1d,30d,whatever)• Needawaytomanagethislifetime

• Waitfor401fromAPI• Renewpriortoexpiration

Page 18: Authentication and authorization in modern JavaScript web applicationssddconf.com/brands/sdd/library/Authentication_and... · 2016. 5. 16. · • Authentication for JavaScript web

UserManager

• JavaScripthelperclasstomanagetokens,expirations,andrenewals• ImplementedintermsofOidcClient

• Partofoidc-client-js library

Page 19: Authentication and authorization in modern JavaScript web applicationssddconf.com/brands/sdd/library/Authentication_and... · 2016. 5. 16. · • Authentication for JavaScript web

Renewingaccesstokens

• Unlikecookies,accesstokensdon'tslide• MustreturntoOIDCOPtoobtainnewaccesstoken

• Startfromscratch• Almostsameasstartingallover• Don'twanttolosethestateintheapp

• Popupwindow• Betterthanstartingover• Somewhatintrusive

• Hiddeniframe• Nicetradeoffforusability

Page 20: Authentication and authorization in modern JavaScript web applicationssddconf.com/brands/sdd/library/Authentication_and... · 2016. 5. 16. · • Authentication for JavaScript web

Summary

• Cookiesaren'tappropriateformodernJavaScriptapps• XSRFissues

• OpenIDConnectistheoneprotocoltorulethemall• Allowsforauthenticationandauthorization

• Client-sideapplicationshavenon-trivialworktodo(dependingonrequirements)• Tokenvalidation• Tokenmanagement