demystifying oauth2 for php

73

Upload: swiftotter-studios

Post on 20-Mar-2017

534 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Demystifying OAuth2 for PHP

WHO DO YOU TRUST WITH YOUR USERNAME AND PASSWORD

WE NEED TO ACCESS DATA IN THE CLOUD

WE DONrsquoT WANT TO STORE THEIR USERNAMEPASSWORD

THERE MUST BE AN ANSWER

OPEN STANDARD FOR AUTHORIZATION V2

The framework for a secure link between

provider customer and us

OAUTH PROVIDERSbull Amazon

bull Dropbox

bull Etsy

bull Evernote

bull Facebook

bull GitHub

bull Google

bull Instagram

bull LinkedIn

bull Microsoft

bull Paypal

bull Reddit

bull SalesForce

bull StackExchange

bull Stripe

bull Trello

bull Twitter

bull Vimeo

bull Yelp

httpsenwikipediaorgwikiList_of_OAuth_providers

OAUTH IShellipbull an Authorization protocol

bull not an Authentication protocol

bull (from the perspective of the web developer)

AUTHORIZATION ldquoI GIVE YOU PERMISSIONrdquo

AUTHENTICATION ldquoI KNOW WHO YOU ARErdquo

AUTHENTICATING USERSbull Can OAuth be used to provide

ldquologin withhelliprdquo

bull NO OAuth is not an

authentication protocol

bull SOLUTION use OpenID Connect

(GoogleMicrosoft) or similar

OAUTH GRANTSbull Authorization Code grant

bull Implicit grant

bull Resource owner credentials grant

bull Client credentials grant

WITHOUT OAUTH2

Web Developer Customer

Provider (ex Google API)

WITH OAUTH

Web Developer Customer

Provider (ex Google API)

OAuth2

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

WHO LIKES 100 GRANDS TWIX

Has stored them safely in escrow

Wants a 100 grand

100 GRAND ESCROW

httpwwwmrwallpapercomhungry-cat-wallpaper

Has decided to share ONE

Wants a 100 grand

100 GRAND ESCROW

100 GRAND ESCROW

Directs mehellip

hellipto Escrow Provider

100 GRAND ESCROW

ldquoIs it ok to sharewith Andrewrdquo

100 GRAND ESCROW

ldquoYesrdquo

100 GRAND ESCROW

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquoldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

PROVIDER (EX GOOGLE)

Web Developer

Customer

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

THE CODESbull Authorization code is short-lived

bull It is the key to determine who the user is and what they gave

access to

bull Access token has a longer life

bull It is the key that gives access to the userrsquos resources

USERNAMEPASSWORD OAUTH2

Has no expiration (unless credentials change)

Access token has expiration

Able to access everything in account

Only can access authorized data

Can be used to maliciously take over an account

Access to data can be revoked at any time

Loosing the usernamepassword can mean all data is compromised

Loosing the access token can mean some data is compromised

THE PROVIDER

Users Developers

Provider

Client ID

Client Secret

Name

Allowed Scopes

Whitelisted Domains

TokensCodes

ID VS SECRETbull Both are for identifying who you are

bull Client ID ldquopublicrdquo key

bull Client Secret ldquoprivaterdquo key never to be sent through

userrsquos browser

AUTHORIZATION SERVERbull Registerslogs invalidates the user

bull Checks the client ID

bull Validates the scopes that we request access to and

ensures those fall within what we originally asked for

bull Asks the user whether it is acceptable to give access

bull Sends the authorization code through the user to us

AUTHORIZATION SERVERbull Looks up the authorization code

bull Generates the access token

bull Returns access token back to us

DO IT YOURSELFhellipbull httpsoauth2thephpleaguecom

bull As always an excellent package by the amazing PHP League

LETrsquoS SEE HOW IT IS DONE

PROVIDER GOOGLE

GOAL ACCESS LIST OF CUSTOMER FILES IN GOOGLE DRIVE

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 2: Demystifying OAuth2 for PHP

WE NEED TO ACCESS DATA IN THE CLOUD

WE DONrsquoT WANT TO STORE THEIR USERNAMEPASSWORD

THERE MUST BE AN ANSWER

OPEN STANDARD FOR AUTHORIZATION V2

The framework for a secure link between

provider customer and us

OAUTH PROVIDERSbull Amazon

bull Dropbox

bull Etsy

bull Evernote

bull Facebook

bull GitHub

bull Google

bull Instagram

bull LinkedIn

bull Microsoft

bull Paypal

bull Reddit

bull SalesForce

bull StackExchange

bull Stripe

bull Trello

bull Twitter

bull Vimeo

bull Yelp

httpsenwikipediaorgwikiList_of_OAuth_providers

OAUTH IShellipbull an Authorization protocol

bull not an Authentication protocol

bull (from the perspective of the web developer)

AUTHORIZATION ldquoI GIVE YOU PERMISSIONrdquo

AUTHENTICATION ldquoI KNOW WHO YOU ARErdquo

AUTHENTICATING USERSbull Can OAuth be used to provide

ldquologin withhelliprdquo

bull NO OAuth is not an

authentication protocol

bull SOLUTION use OpenID Connect

(GoogleMicrosoft) or similar

OAUTH GRANTSbull Authorization Code grant

bull Implicit grant

bull Resource owner credentials grant

bull Client credentials grant

WITHOUT OAUTH2

Web Developer Customer

Provider (ex Google API)

WITH OAUTH

Web Developer Customer

Provider (ex Google API)

OAuth2

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

WHO LIKES 100 GRANDS TWIX

Has stored them safely in escrow

Wants a 100 grand

100 GRAND ESCROW

httpwwwmrwallpapercomhungry-cat-wallpaper

Has decided to share ONE

Wants a 100 grand

100 GRAND ESCROW

100 GRAND ESCROW

Directs mehellip

hellipto Escrow Provider

100 GRAND ESCROW

ldquoIs it ok to sharewith Andrewrdquo

100 GRAND ESCROW

ldquoYesrdquo

100 GRAND ESCROW

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquoldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

PROVIDER (EX GOOGLE)

Web Developer

Customer

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

THE CODESbull Authorization code is short-lived

bull It is the key to determine who the user is and what they gave

access to

bull Access token has a longer life

bull It is the key that gives access to the userrsquos resources

USERNAMEPASSWORD OAUTH2

Has no expiration (unless credentials change)

Access token has expiration

Able to access everything in account

Only can access authorized data

Can be used to maliciously take over an account

Access to data can be revoked at any time

Loosing the usernamepassword can mean all data is compromised

Loosing the access token can mean some data is compromised

THE PROVIDER

Users Developers

Provider

Client ID

Client Secret

Name

Allowed Scopes

Whitelisted Domains

TokensCodes

ID VS SECRETbull Both are for identifying who you are

bull Client ID ldquopublicrdquo key

bull Client Secret ldquoprivaterdquo key never to be sent through

userrsquos browser

AUTHORIZATION SERVERbull Registerslogs invalidates the user

bull Checks the client ID

bull Validates the scopes that we request access to and

ensures those fall within what we originally asked for

bull Asks the user whether it is acceptable to give access

bull Sends the authorization code through the user to us

AUTHORIZATION SERVERbull Looks up the authorization code

bull Generates the access token

bull Returns access token back to us

DO IT YOURSELFhellipbull httpsoauth2thephpleaguecom

bull As always an excellent package by the amazing PHP League

LETrsquoS SEE HOW IT IS DONE

PROVIDER GOOGLE

GOAL ACCESS LIST OF CUSTOMER FILES IN GOOGLE DRIVE

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 3: Demystifying OAuth2 for PHP

WE DONrsquoT WANT TO STORE THEIR USERNAMEPASSWORD

THERE MUST BE AN ANSWER

OPEN STANDARD FOR AUTHORIZATION V2

The framework for a secure link between

provider customer and us

OAUTH PROVIDERSbull Amazon

bull Dropbox

bull Etsy

bull Evernote

bull Facebook

bull GitHub

bull Google

bull Instagram

bull LinkedIn

bull Microsoft

bull Paypal

bull Reddit

bull SalesForce

bull StackExchange

bull Stripe

bull Trello

bull Twitter

bull Vimeo

bull Yelp

httpsenwikipediaorgwikiList_of_OAuth_providers

OAUTH IShellipbull an Authorization protocol

bull not an Authentication protocol

bull (from the perspective of the web developer)

AUTHORIZATION ldquoI GIVE YOU PERMISSIONrdquo

AUTHENTICATION ldquoI KNOW WHO YOU ARErdquo

AUTHENTICATING USERSbull Can OAuth be used to provide

ldquologin withhelliprdquo

bull NO OAuth is not an

authentication protocol

bull SOLUTION use OpenID Connect

(GoogleMicrosoft) or similar

OAUTH GRANTSbull Authorization Code grant

bull Implicit grant

bull Resource owner credentials grant

bull Client credentials grant

WITHOUT OAUTH2

Web Developer Customer

Provider (ex Google API)

WITH OAUTH

Web Developer Customer

Provider (ex Google API)

OAuth2

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

WHO LIKES 100 GRANDS TWIX

Has stored them safely in escrow

Wants a 100 grand

100 GRAND ESCROW

httpwwwmrwallpapercomhungry-cat-wallpaper

Has decided to share ONE

Wants a 100 grand

100 GRAND ESCROW

100 GRAND ESCROW

Directs mehellip

hellipto Escrow Provider

100 GRAND ESCROW

ldquoIs it ok to sharewith Andrewrdquo

100 GRAND ESCROW

ldquoYesrdquo

100 GRAND ESCROW

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquoldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

PROVIDER (EX GOOGLE)

Web Developer

Customer

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

THE CODESbull Authorization code is short-lived

bull It is the key to determine who the user is and what they gave

access to

bull Access token has a longer life

bull It is the key that gives access to the userrsquos resources

USERNAMEPASSWORD OAUTH2

Has no expiration (unless credentials change)

Access token has expiration

Able to access everything in account

Only can access authorized data

Can be used to maliciously take over an account

Access to data can be revoked at any time

Loosing the usernamepassword can mean all data is compromised

Loosing the access token can mean some data is compromised

THE PROVIDER

Users Developers

Provider

Client ID

Client Secret

Name

Allowed Scopes

Whitelisted Domains

TokensCodes

ID VS SECRETbull Both are for identifying who you are

bull Client ID ldquopublicrdquo key

bull Client Secret ldquoprivaterdquo key never to be sent through

userrsquos browser

AUTHORIZATION SERVERbull Registerslogs invalidates the user

bull Checks the client ID

bull Validates the scopes that we request access to and

ensures those fall within what we originally asked for

bull Asks the user whether it is acceptable to give access

bull Sends the authorization code through the user to us

AUTHORIZATION SERVERbull Looks up the authorization code

bull Generates the access token

bull Returns access token back to us

DO IT YOURSELFhellipbull httpsoauth2thephpleaguecom

bull As always an excellent package by the amazing PHP League

LETrsquoS SEE HOW IT IS DONE

PROVIDER GOOGLE

GOAL ACCESS LIST OF CUSTOMER FILES IN GOOGLE DRIVE

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 4: Demystifying OAuth2 for PHP

THERE MUST BE AN ANSWER

OPEN STANDARD FOR AUTHORIZATION V2

The framework for a secure link between

provider customer and us

OAUTH PROVIDERSbull Amazon

bull Dropbox

bull Etsy

bull Evernote

bull Facebook

bull GitHub

bull Google

bull Instagram

bull LinkedIn

bull Microsoft

bull Paypal

bull Reddit

bull SalesForce

bull StackExchange

bull Stripe

bull Trello

bull Twitter

bull Vimeo

bull Yelp

httpsenwikipediaorgwikiList_of_OAuth_providers

OAUTH IShellipbull an Authorization protocol

bull not an Authentication protocol

bull (from the perspective of the web developer)

AUTHORIZATION ldquoI GIVE YOU PERMISSIONrdquo

AUTHENTICATION ldquoI KNOW WHO YOU ARErdquo

AUTHENTICATING USERSbull Can OAuth be used to provide

ldquologin withhelliprdquo

bull NO OAuth is not an

authentication protocol

bull SOLUTION use OpenID Connect

(GoogleMicrosoft) or similar

OAUTH GRANTSbull Authorization Code grant

bull Implicit grant

bull Resource owner credentials grant

bull Client credentials grant

WITHOUT OAUTH2

Web Developer Customer

Provider (ex Google API)

WITH OAUTH

Web Developer Customer

Provider (ex Google API)

OAuth2

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

WHO LIKES 100 GRANDS TWIX

Has stored them safely in escrow

Wants a 100 grand

100 GRAND ESCROW

httpwwwmrwallpapercomhungry-cat-wallpaper

Has decided to share ONE

Wants a 100 grand

100 GRAND ESCROW

100 GRAND ESCROW

Directs mehellip

hellipto Escrow Provider

100 GRAND ESCROW

ldquoIs it ok to sharewith Andrewrdquo

100 GRAND ESCROW

ldquoYesrdquo

100 GRAND ESCROW

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquoldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

PROVIDER (EX GOOGLE)

Web Developer

Customer

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

THE CODESbull Authorization code is short-lived

bull It is the key to determine who the user is and what they gave

access to

bull Access token has a longer life

bull It is the key that gives access to the userrsquos resources

USERNAMEPASSWORD OAUTH2

Has no expiration (unless credentials change)

Access token has expiration

Able to access everything in account

Only can access authorized data

Can be used to maliciously take over an account

Access to data can be revoked at any time

Loosing the usernamepassword can mean all data is compromised

Loosing the access token can mean some data is compromised

THE PROVIDER

Users Developers

Provider

Client ID

Client Secret

Name

Allowed Scopes

Whitelisted Domains

TokensCodes

ID VS SECRETbull Both are for identifying who you are

bull Client ID ldquopublicrdquo key

bull Client Secret ldquoprivaterdquo key never to be sent through

userrsquos browser

AUTHORIZATION SERVERbull Registerslogs invalidates the user

bull Checks the client ID

bull Validates the scopes that we request access to and

ensures those fall within what we originally asked for

bull Asks the user whether it is acceptable to give access

bull Sends the authorization code through the user to us

AUTHORIZATION SERVERbull Looks up the authorization code

bull Generates the access token

bull Returns access token back to us

DO IT YOURSELFhellipbull httpsoauth2thephpleaguecom

bull As always an excellent package by the amazing PHP League

LETrsquoS SEE HOW IT IS DONE

PROVIDER GOOGLE

GOAL ACCESS LIST OF CUSTOMER FILES IN GOOGLE DRIVE

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 5: Demystifying OAuth2 for PHP

OPEN STANDARD FOR AUTHORIZATION V2

The framework for a secure link between

provider customer and us

OAUTH PROVIDERSbull Amazon

bull Dropbox

bull Etsy

bull Evernote

bull Facebook

bull GitHub

bull Google

bull Instagram

bull LinkedIn

bull Microsoft

bull Paypal

bull Reddit

bull SalesForce

bull StackExchange

bull Stripe

bull Trello

bull Twitter

bull Vimeo

bull Yelp

httpsenwikipediaorgwikiList_of_OAuth_providers

OAUTH IShellipbull an Authorization protocol

bull not an Authentication protocol

bull (from the perspective of the web developer)

AUTHORIZATION ldquoI GIVE YOU PERMISSIONrdquo

AUTHENTICATION ldquoI KNOW WHO YOU ARErdquo

AUTHENTICATING USERSbull Can OAuth be used to provide

ldquologin withhelliprdquo

bull NO OAuth is not an

authentication protocol

bull SOLUTION use OpenID Connect

(GoogleMicrosoft) or similar

OAUTH GRANTSbull Authorization Code grant

bull Implicit grant

bull Resource owner credentials grant

bull Client credentials grant

WITHOUT OAUTH2

Web Developer Customer

Provider (ex Google API)

WITH OAUTH

Web Developer Customer

Provider (ex Google API)

OAuth2

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

WHO LIKES 100 GRANDS TWIX

Has stored them safely in escrow

Wants a 100 grand

100 GRAND ESCROW

httpwwwmrwallpapercomhungry-cat-wallpaper

Has decided to share ONE

Wants a 100 grand

100 GRAND ESCROW

100 GRAND ESCROW

Directs mehellip

hellipto Escrow Provider

100 GRAND ESCROW

ldquoIs it ok to sharewith Andrewrdquo

100 GRAND ESCROW

ldquoYesrdquo

100 GRAND ESCROW

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquoldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

PROVIDER (EX GOOGLE)

Web Developer

Customer

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

THE CODESbull Authorization code is short-lived

bull It is the key to determine who the user is and what they gave

access to

bull Access token has a longer life

bull It is the key that gives access to the userrsquos resources

USERNAMEPASSWORD OAUTH2

Has no expiration (unless credentials change)

Access token has expiration

Able to access everything in account

Only can access authorized data

Can be used to maliciously take over an account

Access to data can be revoked at any time

Loosing the usernamepassword can mean all data is compromised

Loosing the access token can mean some data is compromised

THE PROVIDER

Users Developers

Provider

Client ID

Client Secret

Name

Allowed Scopes

Whitelisted Domains

TokensCodes

ID VS SECRETbull Both are for identifying who you are

bull Client ID ldquopublicrdquo key

bull Client Secret ldquoprivaterdquo key never to be sent through

userrsquos browser

AUTHORIZATION SERVERbull Registerslogs invalidates the user

bull Checks the client ID

bull Validates the scopes that we request access to and

ensures those fall within what we originally asked for

bull Asks the user whether it is acceptable to give access

bull Sends the authorization code through the user to us

AUTHORIZATION SERVERbull Looks up the authorization code

bull Generates the access token

bull Returns access token back to us

DO IT YOURSELFhellipbull httpsoauth2thephpleaguecom

bull As always an excellent package by the amazing PHP League

LETrsquoS SEE HOW IT IS DONE

PROVIDER GOOGLE

GOAL ACCESS LIST OF CUSTOMER FILES IN GOOGLE DRIVE

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 6: Demystifying OAuth2 for PHP

The framework for a secure link between

provider customer and us

OAUTH PROVIDERSbull Amazon

bull Dropbox

bull Etsy

bull Evernote

bull Facebook

bull GitHub

bull Google

bull Instagram

bull LinkedIn

bull Microsoft

bull Paypal

bull Reddit

bull SalesForce

bull StackExchange

bull Stripe

bull Trello

bull Twitter

bull Vimeo

bull Yelp

httpsenwikipediaorgwikiList_of_OAuth_providers

OAUTH IShellipbull an Authorization protocol

bull not an Authentication protocol

bull (from the perspective of the web developer)

AUTHORIZATION ldquoI GIVE YOU PERMISSIONrdquo

AUTHENTICATION ldquoI KNOW WHO YOU ARErdquo

AUTHENTICATING USERSbull Can OAuth be used to provide

ldquologin withhelliprdquo

bull NO OAuth is not an

authentication protocol

bull SOLUTION use OpenID Connect

(GoogleMicrosoft) or similar

OAUTH GRANTSbull Authorization Code grant

bull Implicit grant

bull Resource owner credentials grant

bull Client credentials grant

WITHOUT OAUTH2

Web Developer Customer

Provider (ex Google API)

WITH OAUTH

Web Developer Customer

Provider (ex Google API)

OAuth2

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

WHO LIKES 100 GRANDS TWIX

Has stored them safely in escrow

Wants a 100 grand

100 GRAND ESCROW

httpwwwmrwallpapercomhungry-cat-wallpaper

Has decided to share ONE

Wants a 100 grand

100 GRAND ESCROW

100 GRAND ESCROW

Directs mehellip

hellipto Escrow Provider

100 GRAND ESCROW

ldquoIs it ok to sharewith Andrewrdquo

100 GRAND ESCROW

ldquoYesrdquo

100 GRAND ESCROW

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquoldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

PROVIDER (EX GOOGLE)

Web Developer

Customer

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

THE CODESbull Authorization code is short-lived

bull It is the key to determine who the user is and what they gave

access to

bull Access token has a longer life

bull It is the key that gives access to the userrsquos resources

USERNAMEPASSWORD OAUTH2

Has no expiration (unless credentials change)

Access token has expiration

Able to access everything in account

Only can access authorized data

Can be used to maliciously take over an account

Access to data can be revoked at any time

Loosing the usernamepassword can mean all data is compromised

Loosing the access token can mean some data is compromised

THE PROVIDER

Users Developers

Provider

Client ID

Client Secret

Name

Allowed Scopes

Whitelisted Domains

TokensCodes

ID VS SECRETbull Both are for identifying who you are

bull Client ID ldquopublicrdquo key

bull Client Secret ldquoprivaterdquo key never to be sent through

userrsquos browser

AUTHORIZATION SERVERbull Registerslogs invalidates the user

bull Checks the client ID

bull Validates the scopes that we request access to and

ensures those fall within what we originally asked for

bull Asks the user whether it is acceptable to give access

bull Sends the authorization code through the user to us

AUTHORIZATION SERVERbull Looks up the authorization code

bull Generates the access token

bull Returns access token back to us

DO IT YOURSELFhellipbull httpsoauth2thephpleaguecom

bull As always an excellent package by the amazing PHP League

LETrsquoS SEE HOW IT IS DONE

PROVIDER GOOGLE

GOAL ACCESS LIST OF CUSTOMER FILES IN GOOGLE DRIVE

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 7: Demystifying OAuth2 for PHP

OAUTH PROVIDERSbull Amazon

bull Dropbox

bull Etsy

bull Evernote

bull Facebook

bull GitHub

bull Google

bull Instagram

bull LinkedIn

bull Microsoft

bull Paypal

bull Reddit

bull SalesForce

bull StackExchange

bull Stripe

bull Trello

bull Twitter

bull Vimeo

bull Yelp

httpsenwikipediaorgwikiList_of_OAuth_providers

OAUTH IShellipbull an Authorization protocol

bull not an Authentication protocol

bull (from the perspective of the web developer)

AUTHORIZATION ldquoI GIVE YOU PERMISSIONrdquo

AUTHENTICATION ldquoI KNOW WHO YOU ARErdquo

AUTHENTICATING USERSbull Can OAuth be used to provide

ldquologin withhelliprdquo

bull NO OAuth is not an

authentication protocol

bull SOLUTION use OpenID Connect

(GoogleMicrosoft) or similar

OAUTH GRANTSbull Authorization Code grant

bull Implicit grant

bull Resource owner credentials grant

bull Client credentials grant

WITHOUT OAUTH2

Web Developer Customer

Provider (ex Google API)

WITH OAUTH

Web Developer Customer

Provider (ex Google API)

OAuth2

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

WHO LIKES 100 GRANDS TWIX

Has stored them safely in escrow

Wants a 100 grand

100 GRAND ESCROW

httpwwwmrwallpapercomhungry-cat-wallpaper

Has decided to share ONE

Wants a 100 grand

100 GRAND ESCROW

100 GRAND ESCROW

Directs mehellip

hellipto Escrow Provider

100 GRAND ESCROW

ldquoIs it ok to sharewith Andrewrdquo

100 GRAND ESCROW

ldquoYesrdquo

100 GRAND ESCROW

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquoldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

PROVIDER (EX GOOGLE)

Web Developer

Customer

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

THE CODESbull Authorization code is short-lived

bull It is the key to determine who the user is and what they gave

access to

bull Access token has a longer life

bull It is the key that gives access to the userrsquos resources

USERNAMEPASSWORD OAUTH2

Has no expiration (unless credentials change)

Access token has expiration

Able to access everything in account

Only can access authorized data

Can be used to maliciously take over an account

Access to data can be revoked at any time

Loosing the usernamepassword can mean all data is compromised

Loosing the access token can mean some data is compromised

THE PROVIDER

Users Developers

Provider

Client ID

Client Secret

Name

Allowed Scopes

Whitelisted Domains

TokensCodes

ID VS SECRETbull Both are for identifying who you are

bull Client ID ldquopublicrdquo key

bull Client Secret ldquoprivaterdquo key never to be sent through

userrsquos browser

AUTHORIZATION SERVERbull Registerslogs invalidates the user

bull Checks the client ID

bull Validates the scopes that we request access to and

ensures those fall within what we originally asked for

bull Asks the user whether it is acceptable to give access

bull Sends the authorization code through the user to us

AUTHORIZATION SERVERbull Looks up the authorization code

bull Generates the access token

bull Returns access token back to us

DO IT YOURSELFhellipbull httpsoauth2thephpleaguecom

bull As always an excellent package by the amazing PHP League

LETrsquoS SEE HOW IT IS DONE

PROVIDER GOOGLE

GOAL ACCESS LIST OF CUSTOMER FILES IN GOOGLE DRIVE

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 8: Demystifying OAuth2 for PHP

OAUTH IShellipbull an Authorization protocol

bull not an Authentication protocol

bull (from the perspective of the web developer)

AUTHORIZATION ldquoI GIVE YOU PERMISSIONrdquo

AUTHENTICATION ldquoI KNOW WHO YOU ARErdquo

AUTHENTICATING USERSbull Can OAuth be used to provide

ldquologin withhelliprdquo

bull NO OAuth is not an

authentication protocol

bull SOLUTION use OpenID Connect

(GoogleMicrosoft) or similar

OAUTH GRANTSbull Authorization Code grant

bull Implicit grant

bull Resource owner credentials grant

bull Client credentials grant

WITHOUT OAUTH2

Web Developer Customer

Provider (ex Google API)

WITH OAUTH

Web Developer Customer

Provider (ex Google API)

OAuth2

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

WHO LIKES 100 GRANDS TWIX

Has stored them safely in escrow

Wants a 100 grand

100 GRAND ESCROW

httpwwwmrwallpapercomhungry-cat-wallpaper

Has decided to share ONE

Wants a 100 grand

100 GRAND ESCROW

100 GRAND ESCROW

Directs mehellip

hellipto Escrow Provider

100 GRAND ESCROW

ldquoIs it ok to sharewith Andrewrdquo

100 GRAND ESCROW

ldquoYesrdquo

100 GRAND ESCROW

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquoldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

PROVIDER (EX GOOGLE)

Web Developer

Customer

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

THE CODESbull Authorization code is short-lived

bull It is the key to determine who the user is and what they gave

access to

bull Access token has a longer life

bull It is the key that gives access to the userrsquos resources

USERNAMEPASSWORD OAUTH2

Has no expiration (unless credentials change)

Access token has expiration

Able to access everything in account

Only can access authorized data

Can be used to maliciously take over an account

Access to data can be revoked at any time

Loosing the usernamepassword can mean all data is compromised

Loosing the access token can mean some data is compromised

THE PROVIDER

Users Developers

Provider

Client ID

Client Secret

Name

Allowed Scopes

Whitelisted Domains

TokensCodes

ID VS SECRETbull Both are for identifying who you are

bull Client ID ldquopublicrdquo key

bull Client Secret ldquoprivaterdquo key never to be sent through

userrsquos browser

AUTHORIZATION SERVERbull Registerslogs invalidates the user

bull Checks the client ID

bull Validates the scopes that we request access to and

ensures those fall within what we originally asked for

bull Asks the user whether it is acceptable to give access

bull Sends the authorization code through the user to us

AUTHORIZATION SERVERbull Looks up the authorization code

bull Generates the access token

bull Returns access token back to us

DO IT YOURSELFhellipbull httpsoauth2thephpleaguecom

bull As always an excellent package by the amazing PHP League

LETrsquoS SEE HOW IT IS DONE

PROVIDER GOOGLE

GOAL ACCESS LIST OF CUSTOMER FILES IN GOOGLE DRIVE

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 9: Demystifying OAuth2 for PHP

AUTHORIZATION ldquoI GIVE YOU PERMISSIONrdquo

AUTHENTICATION ldquoI KNOW WHO YOU ARErdquo

AUTHENTICATING USERSbull Can OAuth be used to provide

ldquologin withhelliprdquo

bull NO OAuth is not an

authentication protocol

bull SOLUTION use OpenID Connect

(GoogleMicrosoft) or similar

OAUTH GRANTSbull Authorization Code grant

bull Implicit grant

bull Resource owner credentials grant

bull Client credentials grant

WITHOUT OAUTH2

Web Developer Customer

Provider (ex Google API)

WITH OAUTH

Web Developer Customer

Provider (ex Google API)

OAuth2

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

WHO LIKES 100 GRANDS TWIX

Has stored them safely in escrow

Wants a 100 grand

100 GRAND ESCROW

httpwwwmrwallpapercomhungry-cat-wallpaper

Has decided to share ONE

Wants a 100 grand

100 GRAND ESCROW

100 GRAND ESCROW

Directs mehellip

hellipto Escrow Provider

100 GRAND ESCROW

ldquoIs it ok to sharewith Andrewrdquo

100 GRAND ESCROW

ldquoYesrdquo

100 GRAND ESCROW

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquoldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

PROVIDER (EX GOOGLE)

Web Developer

Customer

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

THE CODESbull Authorization code is short-lived

bull It is the key to determine who the user is and what they gave

access to

bull Access token has a longer life

bull It is the key that gives access to the userrsquos resources

USERNAMEPASSWORD OAUTH2

Has no expiration (unless credentials change)

Access token has expiration

Able to access everything in account

Only can access authorized data

Can be used to maliciously take over an account

Access to data can be revoked at any time

Loosing the usernamepassword can mean all data is compromised

Loosing the access token can mean some data is compromised

THE PROVIDER

Users Developers

Provider

Client ID

Client Secret

Name

Allowed Scopes

Whitelisted Domains

TokensCodes

ID VS SECRETbull Both are for identifying who you are

bull Client ID ldquopublicrdquo key

bull Client Secret ldquoprivaterdquo key never to be sent through

userrsquos browser

AUTHORIZATION SERVERbull Registerslogs invalidates the user

bull Checks the client ID

bull Validates the scopes that we request access to and

ensures those fall within what we originally asked for

bull Asks the user whether it is acceptable to give access

bull Sends the authorization code through the user to us

AUTHORIZATION SERVERbull Looks up the authorization code

bull Generates the access token

bull Returns access token back to us

DO IT YOURSELFhellipbull httpsoauth2thephpleaguecom

bull As always an excellent package by the amazing PHP League

LETrsquoS SEE HOW IT IS DONE

PROVIDER GOOGLE

GOAL ACCESS LIST OF CUSTOMER FILES IN GOOGLE DRIVE

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 10: Demystifying OAuth2 for PHP

AUTHENTICATION ldquoI KNOW WHO YOU ARErdquo

AUTHENTICATING USERSbull Can OAuth be used to provide

ldquologin withhelliprdquo

bull NO OAuth is not an

authentication protocol

bull SOLUTION use OpenID Connect

(GoogleMicrosoft) or similar

OAUTH GRANTSbull Authorization Code grant

bull Implicit grant

bull Resource owner credentials grant

bull Client credentials grant

WITHOUT OAUTH2

Web Developer Customer

Provider (ex Google API)

WITH OAUTH

Web Developer Customer

Provider (ex Google API)

OAuth2

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

WHO LIKES 100 GRANDS TWIX

Has stored them safely in escrow

Wants a 100 grand

100 GRAND ESCROW

httpwwwmrwallpapercomhungry-cat-wallpaper

Has decided to share ONE

Wants a 100 grand

100 GRAND ESCROW

100 GRAND ESCROW

Directs mehellip

hellipto Escrow Provider

100 GRAND ESCROW

ldquoIs it ok to sharewith Andrewrdquo

100 GRAND ESCROW

ldquoYesrdquo

100 GRAND ESCROW

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquoldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

PROVIDER (EX GOOGLE)

Web Developer

Customer

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

THE CODESbull Authorization code is short-lived

bull It is the key to determine who the user is and what they gave

access to

bull Access token has a longer life

bull It is the key that gives access to the userrsquos resources

USERNAMEPASSWORD OAUTH2

Has no expiration (unless credentials change)

Access token has expiration

Able to access everything in account

Only can access authorized data

Can be used to maliciously take over an account

Access to data can be revoked at any time

Loosing the usernamepassword can mean all data is compromised

Loosing the access token can mean some data is compromised

THE PROVIDER

Users Developers

Provider

Client ID

Client Secret

Name

Allowed Scopes

Whitelisted Domains

TokensCodes

ID VS SECRETbull Both are for identifying who you are

bull Client ID ldquopublicrdquo key

bull Client Secret ldquoprivaterdquo key never to be sent through

userrsquos browser

AUTHORIZATION SERVERbull Registerslogs invalidates the user

bull Checks the client ID

bull Validates the scopes that we request access to and

ensures those fall within what we originally asked for

bull Asks the user whether it is acceptable to give access

bull Sends the authorization code through the user to us

AUTHORIZATION SERVERbull Looks up the authorization code

bull Generates the access token

bull Returns access token back to us

DO IT YOURSELFhellipbull httpsoauth2thephpleaguecom

bull As always an excellent package by the amazing PHP League

LETrsquoS SEE HOW IT IS DONE

PROVIDER GOOGLE

GOAL ACCESS LIST OF CUSTOMER FILES IN GOOGLE DRIVE

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 11: Demystifying OAuth2 for PHP

AUTHENTICATING USERSbull Can OAuth be used to provide

ldquologin withhelliprdquo

bull NO OAuth is not an

authentication protocol

bull SOLUTION use OpenID Connect

(GoogleMicrosoft) or similar

OAUTH GRANTSbull Authorization Code grant

bull Implicit grant

bull Resource owner credentials grant

bull Client credentials grant

WITHOUT OAUTH2

Web Developer Customer

Provider (ex Google API)

WITH OAUTH

Web Developer Customer

Provider (ex Google API)

OAuth2

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

WHO LIKES 100 GRANDS TWIX

Has stored them safely in escrow

Wants a 100 grand

100 GRAND ESCROW

httpwwwmrwallpapercomhungry-cat-wallpaper

Has decided to share ONE

Wants a 100 grand

100 GRAND ESCROW

100 GRAND ESCROW

Directs mehellip

hellipto Escrow Provider

100 GRAND ESCROW

ldquoIs it ok to sharewith Andrewrdquo

100 GRAND ESCROW

ldquoYesrdquo

100 GRAND ESCROW

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquoldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

PROVIDER (EX GOOGLE)

Web Developer

Customer

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

THE CODESbull Authorization code is short-lived

bull It is the key to determine who the user is and what they gave

access to

bull Access token has a longer life

bull It is the key that gives access to the userrsquos resources

USERNAMEPASSWORD OAUTH2

Has no expiration (unless credentials change)

Access token has expiration

Able to access everything in account

Only can access authorized data

Can be used to maliciously take over an account

Access to data can be revoked at any time

Loosing the usernamepassword can mean all data is compromised

Loosing the access token can mean some data is compromised

THE PROVIDER

Users Developers

Provider

Client ID

Client Secret

Name

Allowed Scopes

Whitelisted Domains

TokensCodes

ID VS SECRETbull Both are for identifying who you are

bull Client ID ldquopublicrdquo key

bull Client Secret ldquoprivaterdquo key never to be sent through

userrsquos browser

AUTHORIZATION SERVERbull Registerslogs invalidates the user

bull Checks the client ID

bull Validates the scopes that we request access to and

ensures those fall within what we originally asked for

bull Asks the user whether it is acceptable to give access

bull Sends the authorization code through the user to us

AUTHORIZATION SERVERbull Looks up the authorization code

bull Generates the access token

bull Returns access token back to us

DO IT YOURSELFhellipbull httpsoauth2thephpleaguecom

bull As always an excellent package by the amazing PHP League

LETrsquoS SEE HOW IT IS DONE

PROVIDER GOOGLE

GOAL ACCESS LIST OF CUSTOMER FILES IN GOOGLE DRIVE

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 12: Demystifying OAuth2 for PHP

OAUTH GRANTSbull Authorization Code grant

bull Implicit grant

bull Resource owner credentials grant

bull Client credentials grant

WITHOUT OAUTH2

Web Developer Customer

Provider (ex Google API)

WITH OAUTH

Web Developer Customer

Provider (ex Google API)

OAuth2

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

WHO LIKES 100 GRANDS TWIX

Has stored them safely in escrow

Wants a 100 grand

100 GRAND ESCROW

httpwwwmrwallpapercomhungry-cat-wallpaper

Has decided to share ONE

Wants a 100 grand

100 GRAND ESCROW

100 GRAND ESCROW

Directs mehellip

hellipto Escrow Provider

100 GRAND ESCROW

ldquoIs it ok to sharewith Andrewrdquo

100 GRAND ESCROW

ldquoYesrdquo

100 GRAND ESCROW

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquoldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

PROVIDER (EX GOOGLE)

Web Developer

Customer

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

THE CODESbull Authorization code is short-lived

bull It is the key to determine who the user is and what they gave

access to

bull Access token has a longer life

bull It is the key that gives access to the userrsquos resources

USERNAMEPASSWORD OAUTH2

Has no expiration (unless credentials change)

Access token has expiration

Able to access everything in account

Only can access authorized data

Can be used to maliciously take over an account

Access to data can be revoked at any time

Loosing the usernamepassword can mean all data is compromised

Loosing the access token can mean some data is compromised

THE PROVIDER

Users Developers

Provider

Client ID

Client Secret

Name

Allowed Scopes

Whitelisted Domains

TokensCodes

ID VS SECRETbull Both are for identifying who you are

bull Client ID ldquopublicrdquo key

bull Client Secret ldquoprivaterdquo key never to be sent through

userrsquos browser

AUTHORIZATION SERVERbull Registerslogs invalidates the user

bull Checks the client ID

bull Validates the scopes that we request access to and

ensures those fall within what we originally asked for

bull Asks the user whether it is acceptable to give access

bull Sends the authorization code through the user to us

AUTHORIZATION SERVERbull Looks up the authorization code

bull Generates the access token

bull Returns access token back to us

DO IT YOURSELFhellipbull httpsoauth2thephpleaguecom

bull As always an excellent package by the amazing PHP League

LETrsquoS SEE HOW IT IS DONE

PROVIDER GOOGLE

GOAL ACCESS LIST OF CUSTOMER FILES IN GOOGLE DRIVE

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 13: Demystifying OAuth2 for PHP

WITHOUT OAUTH2

Web Developer Customer

Provider (ex Google API)

WITH OAUTH

Web Developer Customer

Provider (ex Google API)

OAuth2

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

WHO LIKES 100 GRANDS TWIX

Has stored them safely in escrow

Wants a 100 grand

100 GRAND ESCROW

httpwwwmrwallpapercomhungry-cat-wallpaper

Has decided to share ONE

Wants a 100 grand

100 GRAND ESCROW

100 GRAND ESCROW

Directs mehellip

hellipto Escrow Provider

100 GRAND ESCROW

ldquoIs it ok to sharewith Andrewrdquo

100 GRAND ESCROW

ldquoYesrdquo

100 GRAND ESCROW

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquoldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

PROVIDER (EX GOOGLE)

Web Developer

Customer

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

THE CODESbull Authorization code is short-lived

bull It is the key to determine who the user is and what they gave

access to

bull Access token has a longer life

bull It is the key that gives access to the userrsquos resources

USERNAMEPASSWORD OAUTH2

Has no expiration (unless credentials change)

Access token has expiration

Able to access everything in account

Only can access authorized data

Can be used to maliciously take over an account

Access to data can be revoked at any time

Loosing the usernamepassword can mean all data is compromised

Loosing the access token can mean some data is compromised

THE PROVIDER

Users Developers

Provider

Client ID

Client Secret

Name

Allowed Scopes

Whitelisted Domains

TokensCodes

ID VS SECRETbull Both are for identifying who you are

bull Client ID ldquopublicrdquo key

bull Client Secret ldquoprivaterdquo key never to be sent through

userrsquos browser

AUTHORIZATION SERVERbull Registerslogs invalidates the user

bull Checks the client ID

bull Validates the scopes that we request access to and

ensures those fall within what we originally asked for

bull Asks the user whether it is acceptable to give access

bull Sends the authorization code through the user to us

AUTHORIZATION SERVERbull Looks up the authorization code

bull Generates the access token

bull Returns access token back to us

DO IT YOURSELFhellipbull httpsoauth2thephpleaguecom

bull As always an excellent package by the amazing PHP League

LETrsquoS SEE HOW IT IS DONE

PROVIDER GOOGLE

GOAL ACCESS LIST OF CUSTOMER FILES IN GOOGLE DRIVE

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 14: Demystifying OAuth2 for PHP

WITH OAUTH

Web Developer Customer

Provider (ex Google API)

OAuth2

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

WHO LIKES 100 GRANDS TWIX

Has stored them safely in escrow

Wants a 100 grand

100 GRAND ESCROW

httpwwwmrwallpapercomhungry-cat-wallpaper

Has decided to share ONE

Wants a 100 grand

100 GRAND ESCROW

100 GRAND ESCROW

Directs mehellip

hellipto Escrow Provider

100 GRAND ESCROW

ldquoIs it ok to sharewith Andrewrdquo

100 GRAND ESCROW

ldquoYesrdquo

100 GRAND ESCROW

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquoldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

PROVIDER (EX GOOGLE)

Web Developer

Customer

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

THE CODESbull Authorization code is short-lived

bull It is the key to determine who the user is and what they gave

access to

bull Access token has a longer life

bull It is the key that gives access to the userrsquos resources

USERNAMEPASSWORD OAUTH2

Has no expiration (unless credentials change)

Access token has expiration

Able to access everything in account

Only can access authorized data

Can be used to maliciously take over an account

Access to data can be revoked at any time

Loosing the usernamepassword can mean all data is compromised

Loosing the access token can mean some data is compromised

THE PROVIDER

Users Developers

Provider

Client ID

Client Secret

Name

Allowed Scopes

Whitelisted Domains

TokensCodes

ID VS SECRETbull Both are for identifying who you are

bull Client ID ldquopublicrdquo key

bull Client Secret ldquoprivaterdquo key never to be sent through

userrsquos browser

AUTHORIZATION SERVERbull Registerslogs invalidates the user

bull Checks the client ID

bull Validates the scopes that we request access to and

ensures those fall within what we originally asked for

bull Asks the user whether it is acceptable to give access

bull Sends the authorization code through the user to us

AUTHORIZATION SERVERbull Looks up the authorization code

bull Generates the access token

bull Returns access token back to us

DO IT YOURSELFhellipbull httpsoauth2thephpleaguecom

bull As always an excellent package by the amazing PHP League

LETrsquoS SEE HOW IT IS DONE

PROVIDER GOOGLE

GOAL ACCESS LIST OF CUSTOMER FILES IN GOOGLE DRIVE

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 15: Demystifying OAuth2 for PHP

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

WHO LIKES 100 GRANDS TWIX

Has stored them safely in escrow

Wants a 100 grand

100 GRAND ESCROW

httpwwwmrwallpapercomhungry-cat-wallpaper

Has decided to share ONE

Wants a 100 grand

100 GRAND ESCROW

100 GRAND ESCROW

Directs mehellip

hellipto Escrow Provider

100 GRAND ESCROW

ldquoIs it ok to sharewith Andrewrdquo

100 GRAND ESCROW

ldquoYesrdquo

100 GRAND ESCROW

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquoldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

PROVIDER (EX GOOGLE)

Web Developer

Customer

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

THE CODESbull Authorization code is short-lived

bull It is the key to determine who the user is and what they gave

access to

bull Access token has a longer life

bull It is the key that gives access to the userrsquos resources

USERNAMEPASSWORD OAUTH2

Has no expiration (unless credentials change)

Access token has expiration

Able to access everything in account

Only can access authorized data

Can be used to maliciously take over an account

Access to data can be revoked at any time

Loosing the usernamepassword can mean all data is compromised

Loosing the access token can mean some data is compromised

THE PROVIDER

Users Developers

Provider

Client ID

Client Secret

Name

Allowed Scopes

Whitelisted Domains

TokensCodes

ID VS SECRETbull Both are for identifying who you are

bull Client ID ldquopublicrdquo key

bull Client Secret ldquoprivaterdquo key never to be sent through

userrsquos browser

AUTHORIZATION SERVERbull Registerslogs invalidates the user

bull Checks the client ID

bull Validates the scopes that we request access to and

ensures those fall within what we originally asked for

bull Asks the user whether it is acceptable to give access

bull Sends the authorization code through the user to us

AUTHORIZATION SERVERbull Looks up the authorization code

bull Generates the access token

bull Returns access token back to us

DO IT YOURSELFhellipbull httpsoauth2thephpleaguecom

bull As always an excellent package by the amazing PHP League

LETrsquoS SEE HOW IT IS DONE

PROVIDER GOOGLE

GOAL ACCESS LIST OF CUSTOMER FILES IN GOOGLE DRIVE

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 16: Demystifying OAuth2 for PHP

WHO LIKES 100 GRANDS TWIX

Has stored them safely in escrow

Wants a 100 grand

100 GRAND ESCROW

httpwwwmrwallpapercomhungry-cat-wallpaper

Has decided to share ONE

Wants a 100 grand

100 GRAND ESCROW

100 GRAND ESCROW

Directs mehellip

hellipto Escrow Provider

100 GRAND ESCROW

ldquoIs it ok to sharewith Andrewrdquo

100 GRAND ESCROW

ldquoYesrdquo

100 GRAND ESCROW

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquoldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

PROVIDER (EX GOOGLE)

Web Developer

Customer

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

THE CODESbull Authorization code is short-lived

bull It is the key to determine who the user is and what they gave

access to

bull Access token has a longer life

bull It is the key that gives access to the userrsquos resources

USERNAMEPASSWORD OAUTH2

Has no expiration (unless credentials change)

Access token has expiration

Able to access everything in account

Only can access authorized data

Can be used to maliciously take over an account

Access to data can be revoked at any time

Loosing the usernamepassword can mean all data is compromised

Loosing the access token can mean some data is compromised

THE PROVIDER

Users Developers

Provider

Client ID

Client Secret

Name

Allowed Scopes

Whitelisted Domains

TokensCodes

ID VS SECRETbull Both are for identifying who you are

bull Client ID ldquopublicrdquo key

bull Client Secret ldquoprivaterdquo key never to be sent through

userrsquos browser

AUTHORIZATION SERVERbull Registerslogs invalidates the user

bull Checks the client ID

bull Validates the scopes that we request access to and

ensures those fall within what we originally asked for

bull Asks the user whether it is acceptable to give access

bull Sends the authorization code through the user to us

AUTHORIZATION SERVERbull Looks up the authorization code

bull Generates the access token

bull Returns access token back to us

DO IT YOURSELFhellipbull httpsoauth2thephpleaguecom

bull As always an excellent package by the amazing PHP League

LETrsquoS SEE HOW IT IS DONE

PROVIDER GOOGLE

GOAL ACCESS LIST OF CUSTOMER FILES IN GOOGLE DRIVE

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 17: Demystifying OAuth2 for PHP

Has stored them safely in escrow

Wants a 100 grand

100 GRAND ESCROW

httpwwwmrwallpapercomhungry-cat-wallpaper

Has decided to share ONE

Wants a 100 grand

100 GRAND ESCROW

100 GRAND ESCROW

Directs mehellip

hellipto Escrow Provider

100 GRAND ESCROW

ldquoIs it ok to sharewith Andrewrdquo

100 GRAND ESCROW

ldquoYesrdquo

100 GRAND ESCROW

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquoldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

PROVIDER (EX GOOGLE)

Web Developer

Customer

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

THE CODESbull Authorization code is short-lived

bull It is the key to determine who the user is and what they gave

access to

bull Access token has a longer life

bull It is the key that gives access to the userrsquos resources

USERNAMEPASSWORD OAUTH2

Has no expiration (unless credentials change)

Access token has expiration

Able to access everything in account

Only can access authorized data

Can be used to maliciously take over an account

Access to data can be revoked at any time

Loosing the usernamepassword can mean all data is compromised

Loosing the access token can mean some data is compromised

THE PROVIDER

Users Developers

Provider

Client ID

Client Secret

Name

Allowed Scopes

Whitelisted Domains

TokensCodes

ID VS SECRETbull Both are for identifying who you are

bull Client ID ldquopublicrdquo key

bull Client Secret ldquoprivaterdquo key never to be sent through

userrsquos browser

AUTHORIZATION SERVERbull Registerslogs invalidates the user

bull Checks the client ID

bull Validates the scopes that we request access to and

ensures those fall within what we originally asked for

bull Asks the user whether it is acceptable to give access

bull Sends the authorization code through the user to us

AUTHORIZATION SERVERbull Looks up the authorization code

bull Generates the access token

bull Returns access token back to us

DO IT YOURSELFhellipbull httpsoauth2thephpleaguecom

bull As always an excellent package by the amazing PHP League

LETrsquoS SEE HOW IT IS DONE

PROVIDER GOOGLE

GOAL ACCESS LIST OF CUSTOMER FILES IN GOOGLE DRIVE

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 18: Demystifying OAuth2 for PHP

httpwwwmrwallpapercomhungry-cat-wallpaper

Has decided to share ONE

Wants a 100 grand

100 GRAND ESCROW

100 GRAND ESCROW

Directs mehellip

hellipto Escrow Provider

100 GRAND ESCROW

ldquoIs it ok to sharewith Andrewrdquo

100 GRAND ESCROW

ldquoYesrdquo

100 GRAND ESCROW

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquoldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

PROVIDER (EX GOOGLE)

Web Developer

Customer

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

THE CODESbull Authorization code is short-lived

bull It is the key to determine who the user is and what they gave

access to

bull Access token has a longer life

bull It is the key that gives access to the userrsquos resources

USERNAMEPASSWORD OAUTH2

Has no expiration (unless credentials change)

Access token has expiration

Able to access everything in account

Only can access authorized data

Can be used to maliciously take over an account

Access to data can be revoked at any time

Loosing the usernamepassword can mean all data is compromised

Loosing the access token can mean some data is compromised

THE PROVIDER

Users Developers

Provider

Client ID

Client Secret

Name

Allowed Scopes

Whitelisted Domains

TokensCodes

ID VS SECRETbull Both are for identifying who you are

bull Client ID ldquopublicrdquo key

bull Client Secret ldquoprivaterdquo key never to be sent through

userrsquos browser

AUTHORIZATION SERVERbull Registerslogs invalidates the user

bull Checks the client ID

bull Validates the scopes that we request access to and

ensures those fall within what we originally asked for

bull Asks the user whether it is acceptable to give access

bull Sends the authorization code through the user to us

AUTHORIZATION SERVERbull Looks up the authorization code

bull Generates the access token

bull Returns access token back to us

DO IT YOURSELFhellipbull httpsoauth2thephpleaguecom

bull As always an excellent package by the amazing PHP League

LETrsquoS SEE HOW IT IS DONE

PROVIDER GOOGLE

GOAL ACCESS LIST OF CUSTOMER FILES IN GOOGLE DRIVE

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 19: Demystifying OAuth2 for PHP

100 GRAND ESCROW

Directs mehellip

hellipto Escrow Provider

100 GRAND ESCROW

ldquoIs it ok to sharewith Andrewrdquo

100 GRAND ESCROW

ldquoYesrdquo

100 GRAND ESCROW

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquoldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

PROVIDER (EX GOOGLE)

Web Developer

Customer

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

THE CODESbull Authorization code is short-lived

bull It is the key to determine who the user is and what they gave

access to

bull Access token has a longer life

bull It is the key that gives access to the userrsquos resources

USERNAMEPASSWORD OAUTH2

Has no expiration (unless credentials change)

Access token has expiration

Able to access everything in account

Only can access authorized data

Can be used to maliciously take over an account

Access to data can be revoked at any time

Loosing the usernamepassword can mean all data is compromised

Loosing the access token can mean some data is compromised

THE PROVIDER

Users Developers

Provider

Client ID

Client Secret

Name

Allowed Scopes

Whitelisted Domains

TokensCodes

ID VS SECRETbull Both are for identifying who you are

bull Client ID ldquopublicrdquo key

bull Client Secret ldquoprivaterdquo key never to be sent through

userrsquos browser

AUTHORIZATION SERVERbull Registerslogs invalidates the user

bull Checks the client ID

bull Validates the scopes that we request access to and

ensures those fall within what we originally asked for

bull Asks the user whether it is acceptable to give access

bull Sends the authorization code through the user to us

AUTHORIZATION SERVERbull Looks up the authorization code

bull Generates the access token

bull Returns access token back to us

DO IT YOURSELFhellipbull httpsoauth2thephpleaguecom

bull As always an excellent package by the amazing PHP League

LETrsquoS SEE HOW IT IS DONE

PROVIDER GOOGLE

GOAL ACCESS LIST OF CUSTOMER FILES IN GOOGLE DRIVE

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 20: Demystifying OAuth2 for PHP

100 GRAND ESCROW

ldquoIs it ok to sharewith Andrewrdquo

100 GRAND ESCROW

ldquoYesrdquo

100 GRAND ESCROW

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquoldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

PROVIDER (EX GOOGLE)

Web Developer

Customer

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

THE CODESbull Authorization code is short-lived

bull It is the key to determine who the user is and what they gave

access to

bull Access token has a longer life

bull It is the key that gives access to the userrsquos resources

USERNAMEPASSWORD OAUTH2

Has no expiration (unless credentials change)

Access token has expiration

Able to access everything in account

Only can access authorized data

Can be used to maliciously take over an account

Access to data can be revoked at any time

Loosing the usernamepassword can mean all data is compromised

Loosing the access token can mean some data is compromised

THE PROVIDER

Users Developers

Provider

Client ID

Client Secret

Name

Allowed Scopes

Whitelisted Domains

TokensCodes

ID VS SECRETbull Both are for identifying who you are

bull Client ID ldquopublicrdquo key

bull Client Secret ldquoprivaterdquo key never to be sent through

userrsquos browser

AUTHORIZATION SERVERbull Registerslogs invalidates the user

bull Checks the client ID

bull Validates the scopes that we request access to and

ensures those fall within what we originally asked for

bull Asks the user whether it is acceptable to give access

bull Sends the authorization code through the user to us

AUTHORIZATION SERVERbull Looks up the authorization code

bull Generates the access token

bull Returns access token back to us

DO IT YOURSELFhellipbull httpsoauth2thephpleaguecom

bull As always an excellent package by the amazing PHP League

LETrsquoS SEE HOW IT IS DONE

PROVIDER GOOGLE

GOAL ACCESS LIST OF CUSTOMER FILES IN GOOGLE DRIVE

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 21: Demystifying OAuth2 for PHP

100 GRAND ESCROW

ldquoYesrdquo

100 GRAND ESCROW

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquoldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

PROVIDER (EX GOOGLE)

Web Developer

Customer

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

THE CODESbull Authorization code is short-lived

bull It is the key to determine who the user is and what they gave

access to

bull Access token has a longer life

bull It is the key that gives access to the userrsquos resources

USERNAMEPASSWORD OAUTH2

Has no expiration (unless credentials change)

Access token has expiration

Able to access everything in account

Only can access authorized data

Can be used to maliciously take over an account

Access to data can be revoked at any time

Loosing the usernamepassword can mean all data is compromised

Loosing the access token can mean some data is compromised

THE PROVIDER

Users Developers

Provider

Client ID

Client Secret

Name

Allowed Scopes

Whitelisted Domains

TokensCodes

ID VS SECRETbull Both are for identifying who you are

bull Client ID ldquopublicrdquo key

bull Client Secret ldquoprivaterdquo key never to be sent through

userrsquos browser

AUTHORIZATION SERVERbull Registerslogs invalidates the user

bull Checks the client ID

bull Validates the scopes that we request access to and

ensures those fall within what we originally asked for

bull Asks the user whether it is acceptable to give access

bull Sends the authorization code through the user to us

AUTHORIZATION SERVERbull Looks up the authorization code

bull Generates the access token

bull Returns access token back to us

DO IT YOURSELFhellipbull httpsoauth2thephpleaguecom

bull As always an excellent package by the amazing PHP League

LETrsquoS SEE HOW IT IS DONE

PROVIDER GOOGLE

GOAL ACCESS LIST OF CUSTOMER FILES IN GOOGLE DRIVE

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 22: Demystifying OAuth2 for PHP

100 GRAND ESCROW

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquoldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

PROVIDER (EX GOOGLE)

Web Developer

Customer

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

THE CODESbull Authorization code is short-lived

bull It is the key to determine who the user is and what they gave

access to

bull Access token has a longer life

bull It is the key that gives access to the userrsquos resources

USERNAMEPASSWORD OAUTH2

Has no expiration (unless credentials change)

Access token has expiration

Able to access everything in account

Only can access authorized data

Can be used to maliciously take over an account

Access to data can be revoked at any time

Loosing the usernamepassword can mean all data is compromised

Loosing the access token can mean some data is compromised

THE PROVIDER

Users Developers

Provider

Client ID

Client Secret

Name

Allowed Scopes

Whitelisted Domains

TokensCodes

ID VS SECRETbull Both are for identifying who you are

bull Client ID ldquopublicrdquo key

bull Client Secret ldquoprivaterdquo key never to be sent through

userrsquos browser

AUTHORIZATION SERVERbull Registerslogs invalidates the user

bull Checks the client ID

bull Validates the scopes that we request access to and

ensures those fall within what we originally asked for

bull Asks the user whether it is acceptable to give access

bull Sends the authorization code through the user to us

AUTHORIZATION SERVERbull Looks up the authorization code

bull Generates the access token

bull Returns access token back to us

DO IT YOURSELFhellipbull httpsoauth2thephpleaguecom

bull As always an excellent package by the amazing PHP League

LETrsquoS SEE HOW IT IS DONE

PROVIDER GOOGLE

GOAL ACCESS LIST OF CUSTOMER FILES IN GOOGLE DRIVE

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 23: Demystifying OAuth2 for PHP

100 GRAND ESCROW

ldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoYummyrdquoldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

PROVIDER (EX GOOGLE)

Web Developer

Customer

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

THE CODESbull Authorization code is short-lived

bull It is the key to determine who the user is and what they gave

access to

bull Access token has a longer life

bull It is the key that gives access to the userrsquos resources

USERNAMEPASSWORD OAUTH2

Has no expiration (unless credentials change)

Access token has expiration

Able to access everything in account

Only can access authorized data

Can be used to maliciously take over an account

Access to data can be revoked at any time

Loosing the usernamepassword can mean all data is compromised

Loosing the access token can mean some data is compromised

THE PROVIDER

Users Developers

Provider

Client ID

Client Secret

Name

Allowed Scopes

Whitelisted Domains

TokensCodes

ID VS SECRETbull Both are for identifying who you are

bull Client ID ldquopublicrdquo key

bull Client Secret ldquoprivaterdquo key never to be sent through

userrsquos browser

AUTHORIZATION SERVERbull Registerslogs invalidates the user

bull Checks the client ID

bull Validates the scopes that we request access to and

ensures those fall within what we originally asked for

bull Asks the user whether it is acceptable to give access

bull Sends the authorization code through the user to us

AUTHORIZATION SERVERbull Looks up the authorization code

bull Generates the access token

bull Returns access token back to us

DO IT YOURSELFhellipbull httpsoauth2thephpleaguecom

bull As always an excellent package by the amazing PHP League

LETrsquoS SEE HOW IT IS DONE

PROVIDER GOOGLE

GOAL ACCESS LIST OF CUSTOMER FILES IN GOOGLE DRIVE

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 24: Demystifying OAuth2 for PHP

100 GRAND ESCROW

ldquoYummyrdquoldquoYummyrdquo

Secret wordldquoYummyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

PROVIDER (EX GOOGLE)

Web Developer

Customer

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

THE CODESbull Authorization code is short-lived

bull It is the key to determine who the user is and what they gave

access to

bull Access token has a longer life

bull It is the key that gives access to the userrsquos resources

USERNAMEPASSWORD OAUTH2

Has no expiration (unless credentials change)

Access token has expiration

Able to access everything in account

Only can access authorized data

Can be used to maliciously take over an account

Access to data can be revoked at any time

Loosing the usernamepassword can mean all data is compromised

Loosing the access token can mean some data is compromised

THE PROVIDER

Users Developers

Provider

Client ID

Client Secret

Name

Allowed Scopes

Whitelisted Domains

TokensCodes

ID VS SECRETbull Both are for identifying who you are

bull Client ID ldquopublicrdquo key

bull Client Secret ldquoprivaterdquo key never to be sent through

userrsquos browser

AUTHORIZATION SERVERbull Registerslogs invalidates the user

bull Checks the client ID

bull Validates the scopes that we request access to and

ensures those fall within what we originally asked for

bull Asks the user whether it is acceptable to give access

bull Sends the authorization code through the user to us

AUTHORIZATION SERVERbull Looks up the authorization code

bull Generates the access token

bull Returns access token back to us

DO IT YOURSELFhellipbull httpsoauth2thephpleaguecom

bull As always an excellent package by the amazing PHP League

LETrsquoS SEE HOW IT IS DONE

PROVIDER GOOGLE

GOAL ACCESS LIST OF CUSTOMER FILES IN GOOGLE DRIVE

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 25: Demystifying OAuth2 for PHP

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

PROVIDER (EX GOOGLE)

Web Developer

Customer

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

THE CODESbull Authorization code is short-lived

bull It is the key to determine who the user is and what they gave

access to

bull Access token has a longer life

bull It is the key that gives access to the userrsquos resources

USERNAMEPASSWORD OAUTH2

Has no expiration (unless credentials change)

Access token has expiration

Able to access everything in account

Only can access authorized data

Can be used to maliciously take over an account

Access to data can be revoked at any time

Loosing the usernamepassword can mean all data is compromised

Loosing the access token can mean some data is compromised

THE PROVIDER

Users Developers

Provider

Client ID

Client Secret

Name

Allowed Scopes

Whitelisted Domains

TokensCodes

ID VS SECRETbull Both are for identifying who you are

bull Client ID ldquopublicrdquo key

bull Client Secret ldquoprivaterdquo key never to be sent through

userrsquos browser

AUTHORIZATION SERVERbull Registerslogs invalidates the user

bull Checks the client ID

bull Validates the scopes that we request access to and

ensures those fall within what we originally asked for

bull Asks the user whether it is acceptable to give access

bull Sends the authorization code through the user to us

AUTHORIZATION SERVERbull Looks up the authorization code

bull Generates the access token

bull Returns access token back to us

DO IT YOURSELFhellipbull httpsoauth2thephpleaguecom

bull As always an excellent package by the amazing PHP League

LETrsquoS SEE HOW IT IS DONE

PROVIDER GOOGLE

GOAL ACCESS LIST OF CUSTOMER FILES IN GOOGLE DRIVE

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 26: Demystifying OAuth2 for PHP

100 GRAND ESCROW

ldquoCrunchyrdquo

100 GRAND ESCROW

PROVIDER (EX GOOGLE)

Web Developer

Customer

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

THE CODESbull Authorization code is short-lived

bull It is the key to determine who the user is and what they gave

access to

bull Access token has a longer life

bull It is the key that gives access to the userrsquos resources

USERNAMEPASSWORD OAUTH2

Has no expiration (unless credentials change)

Access token has expiration

Able to access everything in account

Only can access authorized data

Can be used to maliciously take over an account

Access to data can be revoked at any time

Loosing the usernamepassword can mean all data is compromised

Loosing the access token can mean some data is compromised

THE PROVIDER

Users Developers

Provider

Client ID

Client Secret

Name

Allowed Scopes

Whitelisted Domains

TokensCodes

ID VS SECRETbull Both are for identifying who you are

bull Client ID ldquopublicrdquo key

bull Client Secret ldquoprivaterdquo key never to be sent through

userrsquos browser

AUTHORIZATION SERVERbull Registerslogs invalidates the user

bull Checks the client ID

bull Validates the scopes that we request access to and

ensures those fall within what we originally asked for

bull Asks the user whether it is acceptable to give access

bull Sends the authorization code through the user to us

AUTHORIZATION SERVERbull Looks up the authorization code

bull Generates the access token

bull Returns access token back to us

DO IT YOURSELFhellipbull httpsoauth2thephpleaguecom

bull As always an excellent package by the amazing PHP League

LETrsquoS SEE HOW IT IS DONE

PROVIDER GOOGLE

GOAL ACCESS LIST OF CUSTOMER FILES IN GOOGLE DRIVE

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 27: Demystifying OAuth2 for PHP

100 GRAND ESCROW

PROVIDER (EX GOOGLE)

Web Developer

Customer

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

THE CODESbull Authorization code is short-lived

bull It is the key to determine who the user is and what they gave

access to

bull Access token has a longer life

bull It is the key that gives access to the userrsquos resources

USERNAMEPASSWORD OAUTH2

Has no expiration (unless credentials change)

Access token has expiration

Able to access everything in account

Only can access authorized data

Can be used to maliciously take over an account

Access to data can be revoked at any time

Loosing the usernamepassword can mean all data is compromised

Loosing the access token can mean some data is compromised

THE PROVIDER

Users Developers

Provider

Client ID

Client Secret

Name

Allowed Scopes

Whitelisted Domains

TokensCodes

ID VS SECRETbull Both are for identifying who you are

bull Client ID ldquopublicrdquo key

bull Client Secret ldquoprivaterdquo key never to be sent through

userrsquos browser

AUTHORIZATION SERVERbull Registerslogs invalidates the user

bull Checks the client ID

bull Validates the scopes that we request access to and

ensures those fall within what we originally asked for

bull Asks the user whether it is acceptable to give access

bull Sends the authorization code through the user to us

AUTHORIZATION SERVERbull Looks up the authorization code

bull Generates the access token

bull Returns access token back to us

DO IT YOURSELFhellipbull httpsoauth2thephpleaguecom

bull As always an excellent package by the amazing PHP League

LETrsquoS SEE HOW IT IS DONE

PROVIDER GOOGLE

GOAL ACCESS LIST OF CUSTOMER FILES IN GOOGLE DRIVE

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 28: Demystifying OAuth2 for PHP

PROVIDER (EX GOOGLE)

Web Developer

Customer

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

THE CODESbull Authorization code is short-lived

bull It is the key to determine who the user is and what they gave

access to

bull Access token has a longer life

bull It is the key that gives access to the userrsquos resources

USERNAMEPASSWORD OAUTH2

Has no expiration (unless credentials change)

Access token has expiration

Able to access everything in account

Only can access authorized data

Can be used to maliciously take over an account

Access to data can be revoked at any time

Loosing the usernamepassword can mean all data is compromised

Loosing the access token can mean some data is compromised

THE PROVIDER

Users Developers

Provider

Client ID

Client Secret

Name

Allowed Scopes

Whitelisted Domains

TokensCodes

ID VS SECRETbull Both are for identifying who you are

bull Client ID ldquopublicrdquo key

bull Client Secret ldquoprivaterdquo key never to be sent through

userrsquos browser

AUTHORIZATION SERVERbull Registerslogs invalidates the user

bull Checks the client ID

bull Validates the scopes that we request access to and

ensures those fall within what we originally asked for

bull Asks the user whether it is acceptable to give access

bull Sends the authorization code through the user to us

AUTHORIZATION SERVERbull Looks up the authorization code

bull Generates the access token

bull Returns access token back to us

DO IT YOURSELFhellipbull httpsoauth2thephpleaguecom

bull As always an excellent package by the amazing PHP League

LETrsquoS SEE HOW IT IS DONE

PROVIDER GOOGLE

GOAL ACCESS LIST OF CUSTOMER FILES IN GOOGLE DRIVE

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 29: Demystifying OAuth2 for PHP

OAUTH PROCESSbull We redirect user to provider (GoogleFacebooketc)

bull User authorizes us

bull We obtain access token

bull We make requests with access token

THE CODESbull Authorization code is short-lived

bull It is the key to determine who the user is and what they gave

access to

bull Access token has a longer life

bull It is the key that gives access to the userrsquos resources

USERNAMEPASSWORD OAUTH2

Has no expiration (unless credentials change)

Access token has expiration

Able to access everything in account

Only can access authorized data

Can be used to maliciously take over an account

Access to data can be revoked at any time

Loosing the usernamepassword can mean all data is compromised

Loosing the access token can mean some data is compromised

THE PROVIDER

Users Developers

Provider

Client ID

Client Secret

Name

Allowed Scopes

Whitelisted Domains

TokensCodes

ID VS SECRETbull Both are for identifying who you are

bull Client ID ldquopublicrdquo key

bull Client Secret ldquoprivaterdquo key never to be sent through

userrsquos browser

AUTHORIZATION SERVERbull Registerslogs invalidates the user

bull Checks the client ID

bull Validates the scopes that we request access to and

ensures those fall within what we originally asked for

bull Asks the user whether it is acceptable to give access

bull Sends the authorization code through the user to us

AUTHORIZATION SERVERbull Looks up the authorization code

bull Generates the access token

bull Returns access token back to us

DO IT YOURSELFhellipbull httpsoauth2thephpleaguecom

bull As always an excellent package by the amazing PHP League

LETrsquoS SEE HOW IT IS DONE

PROVIDER GOOGLE

GOAL ACCESS LIST OF CUSTOMER FILES IN GOOGLE DRIVE

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 30: Demystifying OAuth2 for PHP

THE CODESbull Authorization code is short-lived

bull It is the key to determine who the user is and what they gave

access to

bull Access token has a longer life

bull It is the key that gives access to the userrsquos resources

USERNAMEPASSWORD OAUTH2

Has no expiration (unless credentials change)

Access token has expiration

Able to access everything in account

Only can access authorized data

Can be used to maliciously take over an account

Access to data can be revoked at any time

Loosing the usernamepassword can mean all data is compromised

Loosing the access token can mean some data is compromised

THE PROVIDER

Users Developers

Provider

Client ID

Client Secret

Name

Allowed Scopes

Whitelisted Domains

TokensCodes

ID VS SECRETbull Both are for identifying who you are

bull Client ID ldquopublicrdquo key

bull Client Secret ldquoprivaterdquo key never to be sent through

userrsquos browser

AUTHORIZATION SERVERbull Registerslogs invalidates the user

bull Checks the client ID

bull Validates the scopes that we request access to and

ensures those fall within what we originally asked for

bull Asks the user whether it is acceptable to give access

bull Sends the authorization code through the user to us

AUTHORIZATION SERVERbull Looks up the authorization code

bull Generates the access token

bull Returns access token back to us

DO IT YOURSELFhellipbull httpsoauth2thephpleaguecom

bull As always an excellent package by the amazing PHP League

LETrsquoS SEE HOW IT IS DONE

PROVIDER GOOGLE

GOAL ACCESS LIST OF CUSTOMER FILES IN GOOGLE DRIVE

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 31: Demystifying OAuth2 for PHP

USERNAMEPASSWORD OAUTH2

Has no expiration (unless credentials change)

Access token has expiration

Able to access everything in account

Only can access authorized data

Can be used to maliciously take over an account

Access to data can be revoked at any time

Loosing the usernamepassword can mean all data is compromised

Loosing the access token can mean some data is compromised

THE PROVIDER

Users Developers

Provider

Client ID

Client Secret

Name

Allowed Scopes

Whitelisted Domains

TokensCodes

ID VS SECRETbull Both are for identifying who you are

bull Client ID ldquopublicrdquo key

bull Client Secret ldquoprivaterdquo key never to be sent through

userrsquos browser

AUTHORIZATION SERVERbull Registerslogs invalidates the user

bull Checks the client ID

bull Validates the scopes that we request access to and

ensures those fall within what we originally asked for

bull Asks the user whether it is acceptable to give access

bull Sends the authorization code through the user to us

AUTHORIZATION SERVERbull Looks up the authorization code

bull Generates the access token

bull Returns access token back to us

DO IT YOURSELFhellipbull httpsoauth2thephpleaguecom

bull As always an excellent package by the amazing PHP League

LETrsquoS SEE HOW IT IS DONE

PROVIDER GOOGLE

GOAL ACCESS LIST OF CUSTOMER FILES IN GOOGLE DRIVE

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 32: Demystifying OAuth2 for PHP

THE PROVIDER

Users Developers

Provider

Client ID

Client Secret

Name

Allowed Scopes

Whitelisted Domains

TokensCodes

ID VS SECRETbull Both are for identifying who you are

bull Client ID ldquopublicrdquo key

bull Client Secret ldquoprivaterdquo key never to be sent through

userrsquos browser

AUTHORIZATION SERVERbull Registerslogs invalidates the user

bull Checks the client ID

bull Validates the scopes that we request access to and

ensures those fall within what we originally asked for

bull Asks the user whether it is acceptable to give access

bull Sends the authorization code through the user to us

AUTHORIZATION SERVERbull Looks up the authorization code

bull Generates the access token

bull Returns access token back to us

DO IT YOURSELFhellipbull httpsoauth2thephpleaguecom

bull As always an excellent package by the amazing PHP League

LETrsquoS SEE HOW IT IS DONE

PROVIDER GOOGLE

GOAL ACCESS LIST OF CUSTOMER FILES IN GOOGLE DRIVE

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 33: Demystifying OAuth2 for PHP

Users Developers

Provider

Client ID

Client Secret

Name

Allowed Scopes

Whitelisted Domains

TokensCodes

ID VS SECRETbull Both are for identifying who you are

bull Client ID ldquopublicrdquo key

bull Client Secret ldquoprivaterdquo key never to be sent through

userrsquos browser

AUTHORIZATION SERVERbull Registerslogs invalidates the user

bull Checks the client ID

bull Validates the scopes that we request access to and

ensures those fall within what we originally asked for

bull Asks the user whether it is acceptable to give access

bull Sends the authorization code through the user to us

AUTHORIZATION SERVERbull Looks up the authorization code

bull Generates the access token

bull Returns access token back to us

DO IT YOURSELFhellipbull httpsoauth2thephpleaguecom

bull As always an excellent package by the amazing PHP League

LETrsquoS SEE HOW IT IS DONE

PROVIDER GOOGLE

GOAL ACCESS LIST OF CUSTOMER FILES IN GOOGLE DRIVE

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 34: Demystifying OAuth2 for PHP

ID VS SECRETbull Both are for identifying who you are

bull Client ID ldquopublicrdquo key

bull Client Secret ldquoprivaterdquo key never to be sent through

userrsquos browser

AUTHORIZATION SERVERbull Registerslogs invalidates the user

bull Checks the client ID

bull Validates the scopes that we request access to and

ensures those fall within what we originally asked for

bull Asks the user whether it is acceptable to give access

bull Sends the authorization code through the user to us

AUTHORIZATION SERVERbull Looks up the authorization code

bull Generates the access token

bull Returns access token back to us

DO IT YOURSELFhellipbull httpsoauth2thephpleaguecom

bull As always an excellent package by the amazing PHP League

LETrsquoS SEE HOW IT IS DONE

PROVIDER GOOGLE

GOAL ACCESS LIST OF CUSTOMER FILES IN GOOGLE DRIVE

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 35: Demystifying OAuth2 for PHP

AUTHORIZATION SERVERbull Registerslogs invalidates the user

bull Checks the client ID

bull Validates the scopes that we request access to and

ensures those fall within what we originally asked for

bull Asks the user whether it is acceptable to give access

bull Sends the authorization code through the user to us

AUTHORIZATION SERVERbull Looks up the authorization code

bull Generates the access token

bull Returns access token back to us

DO IT YOURSELFhellipbull httpsoauth2thephpleaguecom

bull As always an excellent package by the amazing PHP League

LETrsquoS SEE HOW IT IS DONE

PROVIDER GOOGLE

GOAL ACCESS LIST OF CUSTOMER FILES IN GOOGLE DRIVE

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 36: Demystifying OAuth2 for PHP

AUTHORIZATION SERVERbull Looks up the authorization code

bull Generates the access token

bull Returns access token back to us

DO IT YOURSELFhellipbull httpsoauth2thephpleaguecom

bull As always an excellent package by the amazing PHP League

LETrsquoS SEE HOW IT IS DONE

PROVIDER GOOGLE

GOAL ACCESS LIST OF CUSTOMER FILES IN GOOGLE DRIVE

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 37: Demystifying OAuth2 for PHP

DO IT YOURSELFhellipbull httpsoauth2thephpleaguecom

bull As always an excellent package by the amazing PHP League

LETrsquoS SEE HOW IT IS DONE

PROVIDER GOOGLE

GOAL ACCESS LIST OF CUSTOMER FILES IN GOOGLE DRIVE

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 38: Demystifying OAuth2 for PHP

LETrsquoS SEE HOW IT IS DONE

PROVIDER GOOGLE

GOAL ACCESS LIST OF CUSTOMER FILES IN GOOGLE DRIVE

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 39: Demystifying OAuth2 for PHP

PROVIDER GOOGLE

GOAL ACCESS LIST OF CUSTOMER FILES IN GOOGLE DRIVE

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 40: Demystifying OAuth2 for PHP

GOAL ACCESS LIST OF CUSTOMER FILES IN GOOGLE DRIVE

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 41: Demystifying OAuth2 for PHP

httpsgithubcom JosephMaxwell

OAuth2Implementation

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 42: Demystifying OAuth2 for PHP

ONLINE STEPSbull Go to httpconsoledevelopersgooglecom

bull Enable Drive API

bull Create OAuth Credentials

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 43: Demystifying OAuth2 for PHP

CONTINUINGbull Save the file as client_secretsjson in your websitersquos home

directory

bull Change the token_uri attribute to have this value

bull httpswwwgoogleapiscomoauth2v3token

bull Open https[domain_name]manual

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 44: Demystifying OAuth2 for PHP

OAUTH IN PHPhellipldquoIf debugging is the process of removing software bugs

then programming must be the process of putting them inrdquo

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 45: Demystifying OAuth2 for PHP

AUTHORIZATION URLhttpsaccountsgooglecomooauth2auth

response_type=code

ampstate=RANDOM_GENERATED_CODE

ampredirect_uri=[callback_address]

ampscope=httpswwwgoogleapiscomauthdrivereadonly

ampstate=[generated_state_string]

ampclient_id=[client_id]

ampaccess_type=online

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 46: Demystifying OAuth2 for PHP

REFRESH TOKENSbull Refresh tokens are indefinite

bull Access tokens have an expiration

bull Refresh tokens are used to create new access tokens

bull access_type=offline to use refresh tokens

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 47: Demystifying OAuth2 for PHP

USER DOES THEIR MAGIC

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 48: Demystifying OAuth2 for PHP

THE CALLBACKbull Success ldquocoderdquo parameter contains authorization code

bull OpenID State key will be sent back

bull Error ldquoerrorrdquo parameter contains error message

GET authorizecode=4ASDFASDFASDFASDF123123123123 HTTP11 Host developersgooglecom

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 49: Demystifying OAuth2 for PHP

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 50: Demystifying OAuth2 for PHP

$client = new Client() $code = $_GET[code]

$params = [ code =gt $code grant_type =gt authorization_code client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() redirect_uri =gt $this-gthelper-gtgetCallbackUrl(selfAREA) ]

$url = ldquohttpswwwgoogleapiscomoauth2v4tokenrdquo

$response = $client-gtpost($url [form_params =gt $params])

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 51: Demystifying OAuth2 for PHP

access_token1asdf1234asdf1234asdf1234

expires_in3920

token_typeBearer

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 52: Demystifying OAuth2 for PHP

$client = new GuzzleHttpClient() $fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt lsquo[TOKEN_TYPE] [ACCESS_TOKEN]rsquo Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 53: Demystifying OAuth2 for PHP

Posted to httpswwwgoogleapiscomoauth2v4token $params = [ lsquorefresh_token =gt $refreshToken grant_type =gt refresh_token client_id =gt $this-gtconfig-gtgetClientId() client_secret =gt $this-gtconfig-gtgetClientSecret() ]

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 54: Demystifying OAuth2 for PHP

IN A LIBRARYhellipldquoThe best performance improvement is the transition from

the nonworking state to the working staterdquo (J Osterhout)

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 55: Demystifying OAuth2 for PHP

LIBRARYbull The PHP library

bull The PHP League OAuth2 Client

bull httpsgithubcomthephpleagueoauth2-client

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 56: Demystifying OAuth2 for PHP

INITIALIZATION$this-gtprovider = new Google([ clientId =gt $this-gtconfig-gtgetClientId() clientSecret =gt $this-gtconfig-gtgetClientSecret() redirectUri =gt $this-gthelper-gtgetCallbackUrl(selfAREA)])

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 57: Demystifying OAuth2 for PHP

AUTHORIZATION REDIRECT$url = $this-gtprovider-gtgetAuthorizationUrl( [scope =gt $configSCOPE] ) $_SESSION[oauth2_state] = $this-gtprovider-gtgetState() header(Location $url)

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 58: Demystifying OAuth2 for PHP

ACCESS TOKEN$token = $this-gtprovider-gtgetAccessToken( authorization_code [ code =gt $_GET[lsquocode] ] )

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 59: Demystifying OAuth2 for PHP

$fileResponse = $client-gtget( httpswwwgoogleapiscomdrivev2files [ headers =gt [ Authorization =gt $token-gtgetToken() Referer =gt httpoauth2implementationcom ] ] ) $files = new Files($fileResponse-gtgetBody())

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 60: Demystifying OAuth2 for PHP

DObull Protect against common security threats

bull Store random state key in the session and send that to

the provider

bull Store the access token securely

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 61: Demystifying OAuth2 for PHP

ACCESS TOKEN STORAGEbull Do you need to store access token

bull Encrypt it

bull Store it in the session or the DB

bull Maybe Store encryption key as cookie

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 62: Demystifying OAuth2 for PHP

IMPLICIT GRANTbull Used for client-side authorization

bull Access token is public

bull Resource access must be very limited

bull Access token is sent back with first round-trip to

authorization server

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 63: Demystifying OAuth2 for PHP

CLIENT CREDENTIALS GRANTbull Machine-to-machine authentication

bull Agreed-upon signature that has limited permissions

associated with it

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 64: Demystifying OAuth2 for PHP

INDUSTRY TERMINOLOGYbull Client the software we write

bull Resource Server website with which we will interact

bull ex Google API

bull Resource Owner the customer

bull ex the entity who uses our service to access their data

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 65: Demystifying OAuth2 for PHP

OAUTH RESOURCESbull Standard

bull httpstoolsietforghtmlrfc6749

bull Security httpstoolsietforghtmlrfc6819section-53

bull Google API

bull httpsdevelopersgooglecomidentityprotocolsOAuth2hl=en

bull httpsdevelopersgooglecomoauthplayground

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 66: Demystifying OAuth2 for PHP

THE STEPSbull Redirect user to provider (GoogleFacebooketc)

bull Provider authenticates user user authorizes us

bull We exchange authorization code for access token

bull We make requests with access token

QUESTIONS

GO FORTH AND CONNECT

Page 67: Demystifying OAuth2 for PHP

QUESTIONS

GO FORTH AND CONNECT

Page 68: Demystifying OAuth2 for PHP

GO FORTH AND CONNECT