Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for proxy #66

Open
jasonjoh opened this issue Dec 20, 2023 · 1 comment
Open

Add support for proxy #66

jasonjoh opened this issue Dec 20, 2023 · 1 comment

Comments

@jasonjoh
Copy link
Member

Azure Identity libraries in other languages support configuring a proxy in their credential classes. The PHP Graph SDK supports using a proxy, but these auth classes do not.

The PHP League provider supports a proxy: https://oauth2-client.thephpleague.com/usage/#using-a-proxy, so we should be able to support this.

@joaofsa2000
Copy link

joaofsa2000 commented Aug 9, 2024

So i modified 2 class constructers to add a parameter for the oauthprovider with that you can insert a custom config for proxy
as you will see i added the ?AbstractProvider $oauthProvider = null to both constructors

<?php 

class PhpLeagueAuthenticationProviderCustom extends BaseBearerTokenAuthenticationProvider
{
    /**
     * @var PhpLeagueAccessTokenProvider
     */
    private PhpLeagueAccessTokenProvider $accessTokenProvider;

    /**
     * @param TokenRequestContext $tokenRequestContext
     * @param array<string> $scopes
     * @param array<string> $allowedHosts
     */
    public function __construct(TokenRequestContext $tokenRequestContext, array $scopes = [], array $allowedHosts = [], ?AbstractProvider $oauthProvider = null)
    {
        $this->accessTokenProvider = new PhpLeagueAccessTokenProvider($tokenRequestContext, $scopes, $allowedHosts, oauthProvider: $oauthProvider);
        parent::__construct($this->accessTokenProvider);
    }
}

/**
 * Class GraphPhpLeagueAuthenticationProvider
 * @package Microsoft\Graph\Core\Authentication
 * @copyright 2023 Microsoft Corporation
 * @license https://opensource.org/licenses/MIT MIT License
 * @link https://developer.microsoft.com/graph
 * modified for use with custom provider settings
 */
class GraphAuthProviderCustom extends PhpLeagueAuthenticationProviderCustom
{
    /**
     * @param TokenRequestContext $tokenRequestContext
     * @param array<string> $scopes defaults to ["https://[graph national cloud host]/.default"] scope
     * @param string $nationalCloud defaults to https://graph.microsoft.com. See
     * https://learn.microsoft.com/en-us/graph/deployments
     */
    public function __construct(
        TokenRequestContext $tokenRequestContext,
        array $scopes = [],
        string $nationalCloud = NationalCloud::GLOBAL ,
        ?AbstractProvider $oauthProvider = null
    ) {
        $accessTokenProvider = new GraphPhpLeagueAccessTokenProvider($tokenRequestContext, $scopes, $nationalCloud, oauthProvider: $oauthProvider);
        parent::__construct($tokenRequestContext, $scopes, $accessTokenProvider->getAllowedHosts(), $oauthProvider);
    }

}

so now using those 2 new class constructors i just need it to use them

<?php 
use GuzzleHttp\Client as HttpClient;
use Microsoft\Kiota\Authentication\Oauth\ProviderFactory;
use League\OAuth2\Client\Provider\AbstractProvider;
use Microsoft\Graph\Core\Authentication\GraphPhpLeagueAccessTokenProvider;
use Microsoft\Kiota\Abstractions\Authentication\BaseBearerTokenAuthenticationProvider;
use Microsoft\Kiota\Authentication\PhpLeagueAccessTokenProvider;
use Microsoft\Graph\Core\NationalCloud;
use Microsoft\Kiota\Authentication\Oauth\TokenRequestContext;
use Microsoft\Graph\Core\GraphClientFactory;
use Microsoft\Graph\GraphRequestAdapter;
use Microsoft\Graph\GraphServiceClient;
......

$tokenRequestContext = new ClientCredentialContext(
            $tenantId,
            $clientID,
            $clientSecret
        );

        $nationalCloud = NationalCloud::GLOBAL;

        $tokenBaseServiceUrl = GraphPhpLeagueAccessTokenProvider::NATIONAL_CLOUD_TO_AZURE_AD_ENDPOINT[$nationalCloud] ??
            GraphPhpLeagueAccessTokenProvider::NATIONAL_CLOUD_TO_AZURE_AD_ENDPOINT[NationalCloud::GLOBAL];

        $guzzleConfig = [
            'proxy' => "",
            'verify' => false
        ];
// this httpClient is for the authentication as the sdk uses 2 seperate clients for auth and requests
        $httpClient = new HttpClient(
            $guzzleConfig
        );

        $collaborators = ["httpClient" => $httpClient];
// now i create the oauthProvider with the httpclient inside the collaborators parameter
        $oauthProvider = ProviderFactory::create(
            $tokenRequestContext,
            $collaborators,
            $tokenBaseServiceUrl,
            $nationalCloud
        );
        $scopes = ['https://graph.microsoft.com/.default'];
// using the new constructor for the GraphPhpLeagueAuthenticationProvider (GraphAuthProviderCustom) and pass the oauthProvider 
        $authProvider = new GraphAuthProviderCustom($tokenRequestContext, $scopes, oauthProvider: $oauthProvider);

// this is for the requests and i can use the same config for the new client 
        $httpClient = GraphClientFactory::createWithConfig($guzzleConfig);
        $requestAdapter = new GraphRequestAdapter($authProvider, $httpClient);
        $msgraph = GraphServiceClient::createWithRequestAdapter($requestAdapter);

i wish they would make this small change so we dont have to make it ourselves

@github-project-automation github-project-automation bot moved this to Needs Triage 🔍 in Kiota Aug 13, 2024
@andrueastman andrueastman moved this from Needs Triage 🔍 to Todo 📃 in Kiota Aug 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Todo 📃
Development

No branches or pull requests

2 participants