Slide 1

Slide 1 text

Securing  your  APIs  with  OAuth,   OpenID,  and  OpenID  Connect          Manish  Pandit   Silicon  Valley  Code  Camp  2015  

Slide 2

Slide 2 text

About  me     Manish  Pandit   Capital  One,  San  Francisco   @lobster1234     linkedin.com/in/mpandit     slideshare.net/lobster1234    

Slide 3

Slide 3 text

7  years  at  #svcc     OAuth   Social  PlaMorms   PlayFramework!  Java  –  REST  APIs   MongoDB   Introducing  Scala   PlayFramework!  Scala  –  REST  APIs   API  AnUpaVers  

Slide 4

Slide 4 text

APIs   Have  always  been  around     Medium  of  informaUon  exchange     RESTful,  SOAP,  Custom     May  carry  sensi%ve  data  over  the  wire     Can  be  called  on  behalf  of  a  user        

Slide 5

Slide 5 text

API  Security   ThroVling   DoS  ProtecUon   XSS   InjecUons   Access  Control   Transport  Level  Security   IdenUty    

Slide 6

Slide 6 text

API  Security   ThroVling   DoS  ProtecUon   XSS   InjecUons   Access  Control   Transport  Level  Security   IdenUty    

Slide 7

Slide 7 text

Access  Control     Who  can  get  in     Whose  data  they  can  access     What  can  they  access     For  how  long  

Slide 8

Slide 8 text

Typical  Scenario     Online  photo  sharing  website     Allows  users  to  upload  pictures     The  pictures  can  be  flagged  as  private,  or  public     Users  log  in  to  the  website  using  userId/password     The  users  want  to  import  these  pictures  into  their  Facebook    

Slide 9

Slide 9 text

IoT  –  More  players     Fitness  site  tracks  the  number  of  steps  you  take     The  site  also  allows  you  to  track  your  calories  via  a  food  log     Fitness  site  uses  a  NutriUon  website  to  get  calorie  counts     The  user  can  share  his  steps  on  a  Rewards  site,  which  rewards   the  user  based  on  the  steps.     Rewards  site  does  not  care  about  his  food  intake      

Slide 10

Slide 10 text

RESTful  APIs  -­‐  Public     /news     /popular  

Slide 11

Slide 11 text

RESTful  APIs  –  ConfidenUal  ?     /user/12345/status     /photos/12345  

Slide 12

Slide 12 text

Old  Fashioned  Way     Fitness  Site    imports  NutriUon  site’s  database  nightly     Rewards  site  stores  the  users’  credenUals  for  Fitness  Site  in  it’s   database  to  access  their  data     Rewards  site  imports  Fitness  Site’s  data  nightly  for  all  mutual   users    

Slide 13

Slide 13 text

Constraints     Fitness  Site    imports  Nutri/on  site’s  database  nightly     Not  real  Ume   Server-­‐to-­‐server  call   Needs  to  idenUfy  itself  in  order  to  access  data   NutriUon  site  may  want  to  rate-­‐limit  it’s  data  access   There  is  no  idenUty  or  user  associated  with  the  nutriUon  catalog  

Slide 14

Slide 14 text

Constraints     Rewards  site  stores  the  users’  creden/als  for  Fitness  Site  in  it’s   database  to  access  their  data     Rewards  site  can  use  the  Fitness  Site’s  credenUals  to  access  any   data  it  wants  on  the  users’  behalf   In  the  event  of  Rewards  site  geeng  compromised,  the  users  of   Fitness  site  risk  their  credenUals  leaked   Other  than  the  credenUals,  the  Rewards  site  does  not  know  the   idenUty  of  the  user  

Slide 15

Slide 15 text

Constraints     Rewards  site  imports  Fitness  Site’s  data  nightly  for  all  mutual   users     Not  real  Ume   Rewards  site  needs  to  idenUfy  mutual  users  

Slide 16

Slide 16 text

Access  PaVerns   Have  the  Fitness  site  idenUfy  itself  to  the  NutriUon  site     Have  the  Rewards  site  idenUfy  itself  to  the  Fitness  site     Have  the  Rewards  site  users  idenUfy  themselves  to  the  Fitness   site     Have  these  users  grant  or  deny  access  to  finer  grained  data  afer   authenUcaUon  

Slide 17

Slide 17 text

Delegated  AuthenUcaUon     A  (much!)  safer  alternaUve  to  storing  user/password  for  another   site  in  your  database     AuthenUcate  the  user  on  the  site  that  has  both,  his  idenUty  and   his  data     MulUple  idenUUes  –  One  on  Rewards  site,  Another  on  Fitness   site  

Slide 18

Slide 18 text

Delegated  AuthenUcaUon     Authorize  a  service  to  finer  grained  data     The  Fitness  site  user  can  choose  to  grant  access  to  his  steps  to   the  Rewards  site,  not  his  food  log.  

Slide 19

Slide 19 text

Challenges     AuthenUcaUon  at  the  source  of  IdenUty     MulUple  User  IdenUUes     MulUple  applicaUon  or  website  idenUUes     AuthorizaUon,  or  limiUng  the  data  access  at  the  users’  will  

Slide 20

Slide 20 text

DecomposiUon  -­‐  AuthenUcaUon   User  has  credenUals  for  the  Fitness  website     User  has  separate  credenUals  for  the  Rewards  site     User  has  no  idea  about  the  NutriUon  site,  but  the  Fitness  site   does  

Slide 21

Slide 21 text

DecomposiUon  -­‐  AuthorizaUon   User  can  only  access  his  data  on  the  Fitness  site     Fitness  site  can  access  enUre  NutriUon  Catalog  from  the   NutriUon  site     Rewards  site  can  only  access  steps  for  a  user  on  the  Fitness  site,   and  not  his  food  log  

Slide 22

Slide 22 text

DecomposiUon  -­‐  IdenUty   Fitness  Site    is  an  idenUty  provider   (for  users)     Rewards  site  is  an  idenUty  provider   (for  users)     NutriUon  site  is  an  idenUty  provider   (for  other  sites  that  pull  its  catalog)  

Slide 23

Slide 23 text

OAuth     A  protocol  to  allow  for       AuthenUcaUng  the  sites  requesUng  data     DelegaUng  user’s  authenUcaUon  to  the  idenUty  provider     Followed  by  subsequent  authorizaUon     Relies  on  transport  layer  security  for  on-­‐wire  (2.0)  

Slide 24

Slide 24 text

Resource  Owner       A  user  with  data  on  a  (Resource)  server       (Steps  on  fitbit,  Photos  on  Flickr,  Status  updates  on  Facebook,   Tweets  on  TwiVer)  

Slide 25

Slide 25 text

Resource       Data  on  the  Resource  Server  belonging  to  a  user       (Fitbit  steps,  Flickr  photos,  Facebook  updates,  TwiVer  tweets)  

Slide 26

Slide 26 text

Resource  Server       The  server  that  stores  users’  data     (Fitbit,  Flickr,  Facebook,  TwiVer)  

Slide 27

Slide 27 text

AuthorizaUon  Server       The  server  that  can  assert  users’  credenUals     Usually  same  as  the  Resource  Server  (OpenID  teaser!)     (Fitbit,  Flickr,  Facebook,  TwiVer)  

Slide 28

Slide 28 text

Client       Any  applicaUon*  trying  to  access  resources  on  the  resource   server  on  a  resource  owner’s  behalf     (Fitbit,  Flickr,  Facebook,  TwiVer)         *  A  client  can  be  a  resource  server  of  its  own,  and  vice  versa    

Slide 29

Slide 29 text

Access  Tokens     A  proxy  arUfact  for  user  credenUals     Bearer  tokens     A  result  of  an  authorizaUon  step     access_tokens  allow  clients  to  access  a  resource  owner’s  data     access_tokens  expire  afer  a  period  of  Ume     access_tokens  can  be  re-­‐issued  

Slide 30

Slide 30 text

Refresh  Tokens       Used  to  re-­‐request  access_tokens     Have  a  very  long  expiraUon  compared  to  access_tokens     Not    bearer  tokens  

Slide 31

Slide 31 text

OAuth  CredenUals       client_id     client_secret     redirect_uri     These  credenUals  are  set  up  during  client  registraUon  with  the   provider  

Slide 32

Slide 32 text

OAuth  Scopes     Defined  by  the  API  Provider     Can  be  cross  cueng  –  Read/Write/Update/Delete   Can  be  grouped  by  feature  –  steps,  rewards   Can  be  combined  –  Read  steps,  Write  steps  

Slide 33

Slide 33 text

OAuth  Grants           Flows  or  Use  Cases    

Slide 34

Slide 34 text

Client  CredenUals  Grant       Solves  for  the  server-­‐to-­‐server  calls     hVps://api.example.com/token?          grant_type=client_credenUals&          client_id=CLIENT_ID&          client_secret=CLIENT_SECRET  

Slide 35

Slide 35 text

Client  CredenUals  Grant     No  redirect_uri     No  selecUve  granUng  of  scopes     There  is  no  resource  owner,  or  idenUty  involved     Simple  flow,  used  for  server  to  server  calls  via  shared  credenUals     Also  known  as  2-­‐legged  OAuth  

Slide 36

Slide 36 text

Password  Grant       Client  credenUals  and  resource  owner  credenUals  are  used   together  to  get  access  token     hVps://api.example.com/token?          grant_type=password&          username=USERNAME&          password=PASSWORD&          client_id=CLIENT_ID    

Slide 37

Slide 37 text

Password  Grant     Used  for  trusted,  naUve  mobile  apps     No  redirect_uri     No  selecUve  granUng  of  scopes     The  resource  owners’  credenUals  are  captured  by  the  client     The  container  (app)  should  be  guaranteed  to  be  secure  in  order   to  store  resource  owner  credenUals  

Slide 38

Slide 38 text

AuthorizaUon  Code  Grant       Delegated  authenUcaUon  –  the  resource  owner  is  redirected  to   the  idenUty/resource  server  for  authorizaUon,  followed  by  token   exchange     hVps://api.example.com/token?          grant_type=authorizaUon_code&          code=AUTH_CODE_HERE&          redirect_uri=REDIRECT_URI&          client_id=CLIENT_ID&          client_secret=CLIENT_SECRET  

Slide 39

Slide 39 text

AuthorizaUon  Code  Grant     Resource  Owner  is  sent  to  an  authorizaUon_url  with  client_id  and   redirect_uri     Resource  owner  logs  into  the  Resource  Server       Resource  owner  authorizes  the  client  by  granUng  access     Resource  Server  calls  back  the  client  on  a  redirect_uri  with  a  code     The  client  exchanges  this  code  for  an  access_token  and  a   refresh_token  using  the  client_id  and  client_secret    

Slide 40

Slide 40 text

AuthorizaUon  Code  Grant     A  true,  delegated  authenUcaUon     Client  and  Resource  Owner  credenUals  are  asserted  separately     Client  has  to  be  a  server  or  service  (not  browser)     Also  called  3-­‐legged  Oauth     Always  has  a  UX  

Slide 41

Slide 41 text

Implicit  Grant   Resource  Owner  is  sent  to  an  authorizaUon_url  with  client_id   and  redirect_uri     The  client  is  a  browser  or  mobile,  so  no  client_secret.       The  callback_uri  is  a  javascript  callback     Not  a  2-­‐step  process  like  AuthorizaUon  Code     Lesser  used  grant    

Slide 42

Slide 42 text

AuthorizaUon  Code  Grant     Resource  Owner  is  sent  to  an  authorizaUon_url  with  client_id     Resource  owner  logs  into  the  Resource  Server       Resource  owner  authorizes  the  client  by  granUng  access     Resource  Server  calls  back  on  the  redirect_uri  with  access_token   as  a  hash  URL  parameter  

Slide 43

Slide 43 text

OAuth  and  IdenUty         Blurry  lines  

Slide 44

Slide 44 text

OpenID       A  way  to  consolidate  idenUty  by  having  portable  idenUUes     AuthenUcaUon  Protocol     Large  idenUty  providers,  eliminaUng  a  need  for  websites  to  have   their  own  idenUty  stores  

Slide 45

Slide 45 text

OpenID  and  OAuth         OAuth  is  an  authorizaUon  protocol     OpenID  Connect  is  an  authenUcaUon  protocol  built  on  OAuth   (2.0)  

Slide 46

Slide 46 text

OpenID  1.0  and  2.0     XML-­‐based     Has  a  disconnect  with  API  world     Low  adopUon  

Slide 47

Slide 47 text

OpenID  Connect     Third  revision  of  OpenID     Based  on  OAuth  2.0     Much  wider  adopUon     JSON  Based     Interoperable  with  APIs     OAuth  2.0  +  IdenUty  =  OpenID  Connect  

Slide 48

Slide 48 text

OpenID  Connect     IdenUty  as  an  Oauth  2.0  scope     Allows  for  finer  grained  access  to  user  aVributes  (claims)     Provides  an  endpoint  to  get  those  aVributes     Relies  on  JWS  (JSON  Web  Signature)  for  crypto     Relies  on  JWT  (JSON  Web  Token)  to  represent  claims    

Slide 49

Slide 49 text

OpenID  Connect  Default  Scopes     openid     profile     email     address     phone    

Slide 50

Slide 50 text

OpenID  Connect  Claims     Claims  are  finer  grained  aVributes  within  the  scopes     They  can  be  individually  access-­‐controlled  during  the   authenUcaUon  process     email  scope  –  email,  email_verified     profile  scope  –  name,  family  name,  given  name,  gender  

Slide 51

Slide 51 text

OpenID  Connect  ParUes       RP  or  Relying  Party  is  the  one  which  is  requesUng  idenUty     IDP  or  IdenUty  Provider  is  the  one  which  is  asserUng  idenUty      

Slide 52

Slide 52 text

OIDC  Response       Returned  afer  authenUcaUon  step     JWT  standard  (JSON  Web  Token)     Contains  metadata  like  issue  date,  expiraUon,  nonce  along  side   id_token     Can  be  encrypted  via  JWS  (JSON  Web  Signature)     Also  contains  an  access_token  that  can  be  used  for  calling  userinfo      

Slide 53

Slide 53 text

A  JWT   {      "id_token":  "eyJhbGciOiJSUzI1NiIsImtpZCI6IjFlOWdkazcifQ.ewogImlzc          yI6ICJodHRwOi8vc2VydmVyLmV4YW1wbGUuY29tIiwKICJzdWIiOiAiMjQ4Mjg5          NzYxMDAxIiwKICJhdWQiOiAiczZCaGRSa3F0MyIsCiAibm9uY2UiOiAibi0wUzZ          fV3pBMk1qIiwKICJleHAiOiAxMzExMjgxOTcwLAogImlhdCI6IDEzMTEyODA5Nz          AKfQ.ggW8hZ1EuVLuxNuuIJKX_V8a_OMXzR0EHR9R6jgdqrOOF4daGU96Sr_P6q          Jp6IcmD3HP99Obi1PRs-­‐cwh3LO-­‐p146waJ8IhehcwL7F09JdijmBqkvPeB2T9CJ          NqeGpe-­‐gccMg4vfKjkM8FcGvnzZUN4_KSP0aAp1tOJ1zZwgjxqGByKHiOtX7Tpd          QyHE5lcMiKPXfEIQILVq0pc_E2DzL7emopWoaoZTF_m0_N0YzFC6g6EJbOEoRoS          K5hoDalrcvRYLSrQAZZKflyuVCyixEoV9GfNQC3_osjzw2PAithfubEEBLuVVk4          XUVrWOLrLl0nx7RkKU8NXNHq-­‐rvKMzqg"      "access_token":  ”some_token",      "token_type":  "Bearer",      "expires_in":  3600,   }  

Slide 54

Slide 54 text

userinfo         A  userinfo  endpoint  is  accessed  via  an  OIDC  access  token  that  is   returned  as  a  result  of  authenUcaUon     This  call  returns  the  claims  from  the  user’s  profile  that  the  user   has  consented  to  

Slide 55

Slide 55 text

OAuth  and  OpenID  Connect     The  authorizaUon  URL  is  configured  as  a  RP  to  an  OIDC  compliant  IDP     The  user  authenUcates,  resulUng  in  a  JWT  with  id_token  and  an   access_token     The  JWT  is  exchanged  for  an  access_token  or  a  authorizaUon  code     based  on  the  oauth  grant     The  access_token  can  be  then  used  to  invoke  /userinfo  when  needed  

Slide 56

Slide 56 text

      QuesUons?                                      @lobster1234            linkedin.com/in/mpandit