GithubHelp home page GithubHelp logo

webignition / guzzle-http-authentication-middleware Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 1.0 30 KB

Middleware for Guzzle 6 for setting http authentication on all requests sent by a client.

License: MIT License

PHP 100.00%
guzzle-middleware

guzzle-http-authentication-middleware's Introduction

Guzzle HTTP Authentication Middleware

Overview

Middleware for Guzzle 6 for setting basic http authentication on all requests sent by a client.

An authentication header is added to any valid request. A valid request is one where the request host matches a pre-specified domain name.

Useful if your circumstances match all or some of the following:

  • you need to set HTTP authentication on all requests sent by a client for a specific domain only
  • you don't want to specifically add an authorization header to each request made, particularly if there are many points across an application where requests are made
  • you cannot determine in advance to which domains requests might be made and you don't want to leak credentials by means of setting an authorization header on every single request that your client sends

Maybe, just maybe, this is for you.

Usage example

use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use webignition\Guzzle\Middleware\HttpAuthentication\AuthorizationType;
use webignition\Guzzle\Middleware\HttpAuthentication\AuthorizationHeader;
use webignition\Guzzle\Middleware\HttpAuthentication\CredentialsFactory;
use webignition\Guzzle\Middleware\HttpAuthentication\HttpAuthenticationMiddleware;

// Creating a client that uses the middleware
$httpAuthenticationMiddleware = new HttpAuthenticationMiddleware();

$handlerStack = HandlerStack::create();
$handlerStack->push($httpAuthenticationMiddleware, 'http-auth');

$client = new Client([
    'handler' => $handlerStack,
]);

// Setting credentials on the middleware
$basicCredentials = CredentialsFactory::createBasicCredentials('username', 'password');
$httpAuthenticationMiddleware->setType(AuthorizationType::BASIC);
$httpAuthenticationMiddleware->setCredentials($credentials);
$httpAuthenticationMiddleware->setHost('example.com');

// All requests to example.com (or *.example.com) will now have
// a correct Authorization header set for basic HTTP authentication

Application-level considerations

Let's assume you are building a modern PHP application that utilises controllers, services and so on.

Define your HttpAuthenticationMiddleware instance as a service. Use dependency injection to inject that service into whichever part of your application needs to set HTTP authentication credentials. Call HttpAuthenticationMiddleware::setHttpAuthenticationCredentials() as needed, passing in a HttpAuthenticationCredentials instance containing relevant values.

guzzle-http-authentication-middleware's People

Contributors

webignition avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

dunin

guzzle-http-authentication-middleware's Issues

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.