Git Product home page Git Product logo

gsatoken-php's Introduction

Google Service Account - fetching an access token

Sample code in test.php to fetch an access token for scope firebase messaging. For this sample to work you need a Google service account configuration file and name the file serviceaccount.json. The fields used from the service account file are: token_uri, client_email, private_key_id, private_key.

Custom JWTFactory

Implement the interface \GSAToken\JWTFactory to use your own methods of creating a signed JWT. Note! It must support RS256 signed JWT as this is what Google expects.

interface JWTFactory
{
	// Return a string with the signed JWT token
	public function create($header, $claims, $secretkey);
}

class JWTCreator implements \GSAToken\JWTFactory
{
    public function create($header, $claims, $secretkey)
    {
        return createJWT($header, $claims, $secretkey);
    }
}

Custom Http client

Implement the interface \GSAToken\HttpClient to use your own http client to perform the request. An included implementation in class DefaultHttpClient is used by default. It uses curl if available, otherwise file_get_contents.

interface HttpClient 
{
	// Perform a http request
	// Should return an array with these fields
	// array("body" => $response, "httpcode" => $httpcode, "headers" => $responseheaders, "error" => $error);
	public function request($method, $url, $headers, $postdata);
}

Custom token cache

Implement the interface \GSAToken\TokenCache to cache the access token. The included FileTokenCache shows an implementation where the token is encrypted and stored in a file on disk. For example, you may want to store it in a database.

interface TokenCache
{
	public function isValid($now);
	public function getToken();
	public function load();
	public function save($token);
}

Php

The implementation has been tested in php 5.6 and 8.2. Two shell scripts are included to run php cli via docker. This way no need to install php locally. You need docker installed and then pull the php images referenced in the files or you may change to any version you want. Then run:

$ ./php56-cli.sh test.php

gsatoken-php's People

Contributors

deltadecay avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.