32
© 2014 Enservio. All rights reserved. 1 Retrofi8ng OAuth 2.0 Security into Exis?ng REST Service [CON1765] Irena Shaigorodsky Java One, 2014 [email protected] @ishaigorodsky hOps://github.com/ishaigor/restretrosample

JavaOne 2014: Retrofitting OAuth 2.0 Security into Existing REST Services - CON1765

Embed Size (px)

DESCRIPTION

Security is unfortunately a low-priority, nonfunctional requirement and is often overlooked as a result. Reworking it in an existing system is a daunting task, especially when it means providing different levels of security for internal and external calls. The whole undertaking is challenging, but it should not be so. Attending this session will show you a fast and easy way of introducing OAuth 2.0 authentication and authorization into any environment with minimal impact on M2M integrations already in place.

Citation preview

Page 1: JavaOne 2014: Retrofitting OAuth 2.0 Security into Existing REST Services - CON1765

©  2014  Enservio.  All  rights  reserved.  1

Retrofi8ng  OAuth  2.0  Security  into  Exis?ng  REST  Service  [CON1765]  Irena  Shaigorodsky  

Java  One,  2014  

[email protected]  

@ishaigorodsky  hOps://github.com/ishaigor/rest-­‐retro-­‐sample  

Page 2: JavaOne 2014: Retrofitting OAuth 2.0 Security into Existing REST Services - CON1765

©  2014  Enservio.  All  rights  reserved.  2

Quick  Survey  •  How  many  ☐Use  or  plan  to  use  rich  REST  based  UI  for  sensi?ve  informa?on?  ☐Know  what  OAuth  is?  Use  or  plan  to  use  rich  REST  based  UI  with  OAuth?  ☐Use  spring/spring-­‐security/spring-­‐security-­‐oauth?    

Page 3: JavaOne 2014: Retrofitting OAuth 2.0 Security into Existing REST Services - CON1765

©  2014  Enservio.  All  rights  reserved.  3

•  Security  Cost  •  OAuth  2.0  •  Sample  deep-­‐dive  

Agenda  

Page 4: JavaOne 2014: Retrofitting OAuth 2.0 Security into Existing REST Services - CON1765

©  2014  Enservio.  All  rights  reserved.  4

•  Cost  of  security  breach  in  US[1]  –  $188  per  record    –  average  size:  28,765  records  –  customer  loss  

•  Customer  driven  

[1]  2013  Cost  of  Data  Breach  Study:  Global  Analysis  by  Ponemon  Ins?tute©  sponsored  by  Symantec    

Why  My  Company  Needs  Security?    

Page 5: JavaOne 2014: Retrofitting OAuth 2.0 Security into Existing REST Services - CON1765

©  2014  Enservio.  All  rights  reserved.  5

“An  open  protocol  to  allow  secure  authoriza?on  in  a  simple  and  standard  method  from  web,  mobile  and  desktop  applica?ons.”[1]  

 

“The  OAuth  2.0  authoriza?on  framework  enables  a  third-­‐party  applica?on  to  obtain  limited  access  to  an  HTTP  service.”[1]  

 

 [1]  hOp://oauth.net/  

 

OAuth  2.0    

Page 6: JavaOne 2014: Retrofitting OAuth 2.0 Security into Existing REST Services - CON1765

©  2014  Enservio.  All  rights  reserved.  6

•  Resource  –  Resource  Owner  –  Resource  Server  

•  OAuth  2.0  scope  •  OAuth  2.0  client  •  Endpoints  

–  Authoriza?on  Endpoint  –  Token  Endpoint  

•  Tokens  –  Access  Token  –  Refresh  Token  

•  Authoriza?on  Grant  

OAuth  2.0  Lingo    

hOp://wiki.scn.sap.com/wiki/display/Security/OAuth+2.0+Terminology  

Page 7: JavaOne 2014: Retrofitting OAuth 2.0 Security into Existing REST Services - CON1765

©  2014  Enservio.  All  rights  reserved.  7

•  Authoriza*on  Code  Grant  Flow  –  Google  –  Facebook  

•  Resource  Owner  Password  Creden?al  Flow  •  Client  Creden?al  Flow  •  Implicit  Grant  Flow  

–  JavaScript  client  

OAuth  2.0  Flows    

Page 8: JavaOne 2014: Retrofitting OAuth 2.0 Security into Existing REST Services - CON1765

©  2014  Enservio.  All  rights  reserved.  8

Securing  REST  calls:  OAuth  2.0    

hOp://docs.oracle.com/cd/E39820_01/doc.11121/gateway_docs/content/images/oauth/oauth_web_server_flow.png  

•  Authoriza?on  Code  Grant  Flow  

Page 9: JavaOne 2014: Retrofitting OAuth 2.0 Security into Existing REST Services - CON1765

©  2014  Enservio.  All  rights  reserved.  9

•  Authoriza?on  Code  Grant  Flow  –  Google  –  Facebook  

•  Resource  Owner  Password  Creden*al  Flow  •  Client  Creden?al  Flow  •  Implicit  Grant  Flow  

–  JavaScript  client  

OAuth  2.0  Flows    

Page 10: JavaOne 2014: Retrofitting OAuth 2.0 Security into Existing REST Services - CON1765

©  2014  Enservio.  All  rights  reserved.  10

•  Resource  Owner  Password  Creden?al  Flow  

Securing  REST  calls:  OAuth  2.0    

hOp://docs.oracle.com/cd/E39820_01/doc.11121/gateway_docs/content/images/oauth/oauth_username_password_flow.png  

Page 11: JavaOne 2014: Retrofitting OAuth 2.0 Security into Existing REST Services - CON1765

©  2014  Enservio.  All  rights  reserved.  11

•  Authoriza?on  Code  Grant  Flow  –  Google  –  Facebook  

•  Resource  Owner  Password  Creden?al  Flow  •  Client  Creden*al  Flow  •  Implicit  Grant  Flow  

–  JavaScript  client  

OAuth  2.0  Flows    

Page 12: JavaOne 2014: Retrofitting OAuth 2.0 Security into Existing REST Services - CON1765

©  2014  Enservio.  All  rights  reserved.  12

•  Client  Creden?al  Flow  

Securing  REST  calls:  OAuth  2.0    

hOp://docs.oracle.com/cd/E39820_01/doc.11121/gateway_docs/content/images/oauth/oauth_client_creden?als_flow.png  

Page 13: JavaOne 2014: Retrofitting OAuth 2.0 Security into Existing REST Services - CON1765

©  2014  Enservio.  All  rights  reserved.  13

•  Authoriza?on  Code  Grant  Flow  –  Google  –  Facebook  

•  Resource  Owner  Password  Creden?al  Flow  •  Client  Creden?al  Flow  •  Implicit  Grant  Flow  

–  JavaScript  client  

OAuth  2.0  Flows    

Page 14: JavaOne 2014: Retrofitting OAuth 2.0 Security into Existing REST Services - CON1765

©  2014  Enservio.  All  rights  reserved.  14

•  Implicit  Grant  Flow  

Securing  REST  calls:  OAuth  2.0    

hOp://docs.oracle.com/cd/E39820_01/doc.11121/gateway_docs/content/images/oauth/oauth_user_agent_flow.png  

Page 15: JavaOne 2014: Retrofitting OAuth 2.0 Security into Existing REST Services - CON1765

©  2014  Enservio.  All  rights  reserved.  15

hOps://github.com/ishaigor/rest-­‐retro-­‐sample  

•  Unprotected  JavaScript  Widget  –  Unprotected  REST  Words  Service    

•  Spring  MVC  –  Legacy  protected  JSP    /  JavaScript  Widget  

•  Spring  Security  •  AngularJS  

•  Protected  Widget  –  Protected  service  

•  Spring  Security  OAuth  –  Protected  client  

•  Spring  Security  Oauth  

–  HTTP  Authoriza?on  Header  

•  Protected  gateway  –  Spring  Integra?on  –  Customiza?on  

Sample  deep-­‐dive    

Page 16: JavaOne 2014: Retrofitting OAuth 2.0 Security into Existing REST Services - CON1765

©  2014  Enservio.  All  rights  reserved.  16

•  @RestController  

Meet  the  unprotected  REST  Service    (Spring  MVC)  

Page 17: JavaOne 2014: Retrofitting OAuth 2.0 Security into Existing REST Services - CON1765

©  2014  Enservio.  All  rights  reserved.  17

•  ng-­‐infinite-­‐scroll  •  AbstractDispatcherServletIni?alizer  

–  springSecurityFilterChain  

•  WebSecurityConfigurerAdapter  –  @EnableWebSecurity  –  Authen?ca?onManagerBuilder  –  WebSecurity  –  HOpSecurity  

•  Persistence  –  Data  source  –  Group  authori?es  by  user  name  

Meet  secure  legacy  client  with  unprotected  Rich  UI  (Spring  Security,  Spring  MVC,  AngularJS)  

Page 18: JavaOne 2014: Retrofitting OAuth 2.0 Security into Existing REST Services - CON1765

©  2014  Enservio.  All  rights  reserved.  18

Spring  Security:  User  Details  

Page 19: JavaOne 2014: Retrofitting OAuth 2.0 Security into Existing REST Services - CON1765

©  2014  Enservio.  All  rights  reserved.  19

•  <%@  taglib  prefix="authz"  uri="hOp://www.springframework.org/security/tags"%>  

•  <authz:authorize  ifAllGranted="ROLE_USER">…</authz:authorize>  

Meet  secure  legacy  client  with  unprotected  Rich  UI  (Spring  Security,  Spring  MVC,  AngularJS)  –cont’d  

Page 20: JavaOne 2014: Retrofitting OAuth 2.0 Security into Existing REST Services - CON1765

©  2014  Enservio.  All  rights  reserved.  20

hOps://github.com/ishaigor/rest-­‐retro-­‐sample  

•  Unprotected  JavaScript  Widget  –  Unprotected  REST  Words  Service    

•  Spring  MVC  –  Legacy  protected  JSP    /  JavaScript  Widget  

•  Spring  Security  •  AngularJS  

•  Protected  Widget  –  Protected  service  

•  Spring  Security  OAuth  –  Protected  client  

•  Spring  Security  Oauth  

–  HTTP  Authoriza?on  Header  

•  Protected  gateway  –  Spring  Integra?on  –  Customiza?on  

Sample  deep-­‐dive    

Page 21: JavaOne 2014: Retrofitting OAuth 2.0 Security into Existing REST Services - CON1765

©  2014  Enservio.  All  rights  reserved.  21

•  Authoriza?onServerConfigurerAdapter  –  ClientDetailsServiceConfigurer  –  @EnableAuthoriza?onServer  –  Authoriza?onServerEndpointsConfigurer  –  Authoriza?onServerSecurityConfigurer  

•  GlobalMethodSecurityConfigura?on  –  @EnableGlobalMethodSecurity  –  OAuth2MethodSecurityExpressionHandler  

Protected  Service  (Spring  Security,  Spring  MVC)  

Page 22: JavaOne 2014: Retrofitting OAuth 2.0 Security into Existing REST Services - CON1765

©  2014  Enservio.  All  rights  reserved.  22

•  ResourceServerConfigurerAdapter  –  ResourceServerSecurityConfigurer  –  HOpSecurity  

•  .csrf().requireCsrfProtec?onMatcher(new  AntPathRequestMatcher("/oauth/authorize")).disable()  

•  Persistence  –  TokenStore  –  ClientTokenServices  –  Authoriza?onCodeServices  –  ApprovalStore  

•  ApprovalStoreUserApprovalHandler  

Protected  Service  (Spring  Security,  Spring  MVC)  –  cont’d  

Page 23: JavaOne 2014: Retrofitting OAuth 2.0 Security into Existing REST Services - CON1765

©  2014  Enservio.  All  rights  reserved.  23

Protected  Service  (Spring  Security,  Spring  MVC)  –  cont’d  

Page 24: JavaOne 2014: Retrofitting OAuth 2.0 Security into Existing REST Services - CON1765

©  2014  Enservio.  All  rights  reserved.  24

•  @BeforeOAuth2Context  

•  @OAuth2ContextConfigura?on  

•  BaseOAuth2ProtectedResourceDetails  •  Integra?onTest  •  Integra?onTestHelper  

Protected  Service  (Spring  Security,  Spring  MVC):  tes?ng  

Page 25: JavaOne 2014: Retrofitting OAuth 2.0 Security into Existing REST Services - CON1765

©  2014  Enservio.  All  rights  reserved.  25

•  Authen?ca?onManager  –  eraseCreden?als  

•  Applica?onListener<AbstractAuthen?ca?onEvent>  –  ResourceOwnerPasswordAccessTokenProvider  

•  CustomAuthen?ca?onDetailsSource  –  CustomAuthen?ca?onDetails  –  WebAuthen?ca?onDetailsSource  

Protected  client,  protected  Rich  UI  (Spring  Security,  Spring  MVC,  Spring  Security  OAuth  2.0)  

Page 26: JavaOne 2014: Retrofitting OAuth 2.0 Security into Existing REST Services - CON1765

©  2014  Enservio.  All  rights  reserved.  26

•  Limita?ons:  –  Added  security  overhead  –  No  unprotected  internal  access  

Protected  service  with  Spring    

Page 27: JavaOne 2014: Retrofitting OAuth 2.0 Security into Existing REST Services - CON1765

©  2014  Enservio.  All  rights  reserved.  27

hOps://github.com/ishaigor/rest-­‐retro-­‐sample  

•  Unprotected  JavaScript  Widget  –  Unprotected  REST  Words  Service    

•  Spring  MVC  –  Legacy  protected  JSP    /  JavaScript  Widget  

•  Spring  Security  •  AngularJS  

•  Protected  Widget  –  Protected  service  

•  Spring  Security  OAuth  –  Protected  client  

•  Spring  Security  Oauth  

–  HTTP  Authoriza?on  Header  

•  Protected  gateway  –  Spring  Integra?on  –  Customiza?on  

Sample  deep-­‐dive  

Page 28: JavaOne 2014: Retrofitting OAuth 2.0 Security into Existing REST Services - CON1765

©  2014  Enservio.  All  rights  reserved.  28

•  int-­‐hOp:inbound-­‐gateway  •  int-­‐hOp:outbound-­‐gateway  •  int:channel  •  int:annota?on-­‐config  •  int-­‐jmx:mbean-­‐export  

Security  Gateway  Pass  Through  with  Spring  Integra?on  

Page 29: JavaOne 2014: Retrofitting OAuth 2.0 Security into Existing REST Services - CON1765

©  2014  Enservio.  All  rights  reserved.  29

•  OutboundHeaderMapper  

•  RangeEnforcer  •  CustomOAuth2WebSecurityExpressionHandler  

•  CustomSecurityExpressionMethods  

•  ClientHOpRequestFactory  

Security  Gateway  Pass  Through  with  Spring  Integra?on:  customiza?on  

Page 30: JavaOne 2014: Retrofitting OAuth 2.0 Security into Existing REST Services - CON1765

©  2014  Enservio.  All  rights  reserved.  30

•  hOp://oauth.net/2/  •  hOp://projects.spring.io/spring-­‐security/    •  hOp://projects.spring.io/spring-­‐security-­‐oauth/  •  hOps://github.com/ishaigor/rest-­‐retro-­‐sample  

•  hOp://binarymuse.github.io/ngInfiniteScroll/        

Resources  

Page 31: JavaOne 2014: Retrofitting OAuth 2.0 Security into Existing REST Services - CON1765

©  2014  Enservio.  All  rights  reserved.  31

OAuth 2.0 Bearer for JavaScript /external REST IdP with SSO

WS-Security /SAML for SOAP

Digest / Signatures Encryption

OAuth 2.0 SAML OAuth 2.0 MAC

Security  Roadmap  

Address  REST  Services  Exposure  

Merge  user  iden??es  in  a  single  

directory  

Centralize  iden?ty  management  

Build  secure  APIs  with  our  customers  

Other  enhancements  

Page 32: JavaOne 2014: Retrofitting OAuth 2.0 Security into Existing REST Services - CON1765

©  2014  Enservio.  All  rights  reserved.  32