Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
williammck committed Jan 22, 2023
0 parents commit b8ef340
Show file tree
Hide file tree
Showing 12 changed files with 746 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/.github export-ignore
/test export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/phpunit.xml.dist export-ignore
52 changes: 52 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: test

on:
pull_request:
push:
branches: [ main ]

jobs:
run:
runs-on: ubuntu-22.04
strategy:
fail-fast: false
matrix:
php:
- '7.4'
- '8.0'
- '8.1'
- '8.2'
minimum_versions: [false]
include:
- description: 'Minimum version'
php: '7.4'
minimum_versions: true

name: PHP ${{ matrix.php }} ${{ matrix.description }}
steps:
- name: Checkout
uses: actions/checkout@v2

- uses: actions/cache@v2
with:
path: ~/.composer/cache/files
key: ${{ matrix.php }}

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}

- name: Install dependencies
run: composer install
if: matrix.minimum_versions == false

- name: Install dependencies lower
run: composer update --no-interaction --prefer-lowest
if: matrix.minimum_versions == true

- name: Run PHPCS linter
run: composer lint

- name: Run PHPUnit tests
run: composer test
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
coverage/
vendor/
.phpunit.result.cache
clover.xml
composer.lock
composer.phar
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# OAuth 2.0 VATSIM Provider Changelog

## 1.0.0 - 2023-01-22

- Initial release
42 changes: 42 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Contributing

Contributions are welcome and encouraged!

We accept contributions via Pull Requests on [GitHub](https://github.com/vatsimnetwork/oauth2-vatsim-php).


## Pull Requests

- **[PSR-12 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-12-extended-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://github.com/squizlabs/PHP_CodeSniffer).

- **Add tests!** - Your patch won't be accepted if it doesn't have tests.

- **Document any change in behaviour** - Make sure the README and any other relevant documentation are kept up-to-date.

- **Consider our release cycle** - We try to follow SemVer. Randomly breaking public APIs is not an option.

- **Create topic branches** - Don't ask us to pull from your main branch.

- **One pull request per feature** - If you want to do more than one thing, please send multiple pull requests.

- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please squash them before submitting.

- **Ensure tests pass!** - Please run the tests (see below) before submitting your pull request, and make sure they pass. We won't accept a patch until all tests pass.

- **Ensure no coding standards violations** - Please run PHP Code Sniffer using the PSR-12 standard (see below) before submitting your pull request. A violation will cause the build to fail, so please make sure there are no violations. We can't accept a patch if the build fails.


## Running Tests

```sh
composer test
```


## Running PHP Code Sniffer

```sh
composer lint
```

**Happy coding**!
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2023 William McKinnerney <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
76 changes: 76 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# VATSIM Connect Provider for OAuth 2.0 Client

[![Build Status](https://img.shields.io/github/workflow/status/vatsimnetwork/oauth2-vatsim-php/test/main)](https://github.com/vatsimnetwork/oauth2-vatsim-php/actions/workflows/test.yaml)
[![License](https://img.shields.io/packagist/l/vatsim/oauth2-vatsim)](https://github.com/vatsimnetwork/oauth2-vatsim-php/blob/main/LICENSE)
[![Latest Stable Version](https://img.shields.io/packagist/v/vatsim/oauth2-vatsim)](https://packagist.org/packages/vatsim/oauth2-vatsim)

This package provides VATSIM Connect support for the PHP League's [OAuth 2.0 Client](https://github.com/thephpleague/oauth2-client).

## Requirements

To use this package, it will be necessary to have a VATSIM Connect client ID and client
secret. These are referred to as `{vatsim-client-id}` and `{vatsim-client-secret}`
in the documentation.

Please follow the [VATSIM Connect instructions][oauth-setup] to create the required credentials.

[oauth-setup]: https://github.com/vatsimnetwork/developer-info/wiki/Connect

## Installation

To install, use composer:

```sh
composer require vatsim/oauth2-vatsim
```

## Usage

Usage is the same as [The League's OAuth client](league-usage), using `\Vatsim\OAuth2\Client\Provider\Vatsim` as the provider.

[league-usage]: https://oauth2-client.thephpleague.com/usage/

### Available Options

The `Vatsim` provider has the following options:

- `domain` allows for switching between the prod or dev Connect instance (default: `https://auth.vatsim.net`)

### Scopes

At time of writing, [available scopes][scopes] are:
- `full_name` - Full name (first and last)
- `email`- Email address
- `country` - Residence country
- `vatsim_details` - VATSIM pilot rating, ATC rating, region, division, sub-division

[scopes]: https://github.com/vatsimnetwork/developer-info/wiki/Connect-Redirect-the-User

## Testing

Tests can be run with:

```sh
composer test
```

Style checks can be run with:

```sh
composer lint
```

## Contributing

Please see [CONTRIBUTING.md](https://github.com/vatsimnetwork/oauth2-vatsim-php/blob/main/CONTRIBUTING.md) for details.


## Credits

- [William McKinnerney](https://williammck.net)
- [All Contributors](https://github.com/vatsimnetwork/oauth2-vatsim-php/contributors)


## License

The MIT License (MIT). Please see the [License File](https://github.com/vatsimnetwork/oauth2-vatsim-php/blob/main/LICENSE) for more information.
46 changes: 46 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "vatsim/oauth2-vatsim",
"description": "VATSIM OAuth 2.0 Client Provider for The PHP League OAuth2-Client",
"license": "MIT",
"authors": [
{
"name": "William McKinnerney",
"email": "[email protected]",
"homepage": "https://williammck.net"
}
],
"keywords": [
"oauth",
"oauth2",
"client",
"authorization",
"authentication",
"vatsim"
],
"minimum-stability": "stable",
"require": {
"php": "^7.4 || ^8.0",
"league/oauth2-client": "^2.0"
},
"require-dev": {
"ext-json": "*",
"mockery/mockery": "^1.4",
"phpunit/phpunit": "^9.3",
"squizlabs/php_codesniffer": "^3.5"
},
"autoload": {
"psr-4": {
"Vatsim\\OAuth2\\Client\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Vatsim\\OAuth2\\Client\\Test\\": "test/src/"
}
},
"scripts": {
"lint": "phpcs src/ test/ --standard=PSR12 -sp",
"test": "phpunit",
"test-coverage": "phpunit --coverage-html coverage/"
}
}
17 changes: 17 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="all">
<directory>./test</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
</phpunit>
117 changes: 117 additions & 0 deletions src/Provider/Vatsim.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

namespace Vatsim\OAuth2\Client\Provider;

use League\OAuth2\Client\Provider\AbstractProvider;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
use League\OAuth2\Client\Token\AccessToken;
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
use Psr\Http\Message\ResponseInterface;

class Vatsim extends AbstractProvider
{
use BearerAuthorizationTrait;

/**
* VATSIM Connect API base URL.
*
* @var string
*/
public string $domain = 'https://auth.vatsim.net';

/**
* Returns the base URL for authorizing a client.
*
* @return string
*/
public function getBaseAuthorizationUrl(): string
{
return $this->domain . '/oauth/authorize';
}

/**
* Returns the base URL for requesting an access token.
*
* @param array $params
* @return string
*/
public function getBaseAccessTokenUrl(array $params): string
{
return $this->domain . '/oauth/token';
}

/**
* Returns the URL for requesting the resource owner's details.
*
* @param AccessToken $token
* @return string
*/
public function getResourceOwnerDetailsUrl(AccessToken $token): string
{
return $this->domain . '/api/user';
}

/**
* Returns the default scopes used by this provider.
*
* This should only be the scopes that are required to request the details
* of the resource owner, rather than all the available scopes.
*
* @return array
*/
protected function getDefaultScopes(): array
{
return [];
}

/**
* Returns the string that should be used to separate scopes when building
* the URL for requesting an access token.
*
* @return string Scope separator
*/
protected function getScopeSeparator(): string
{
return ' ';
}

/**
* Checks a provider response for errors.
*
* @throws IdentityProviderException
* @param ResponseInterface $response
* @param array|string $data Parsed response data
* @return void
*/
protected function checkResponse(ResponseInterface $response, $data): void
{
if ($response->getStatusCode() >= 400) {
throw new IdentityProviderException(
$data['message'] ?? $response->getReasonPhrase(),
$response->getStatusCode(),
$data
);
}

if (isset($data['error'])) {
throw new IdentityProviderException(
$data['error'],
$response->getStatusCode(),
$data
);
}
}

/**
* Generates a resource owner object from a successful resource owner
* details request.
*
* @param array $response
* @param AccessToken $token
* @return VatsimResourceOwner
*/
protected function createResourceOwner(array $response, AccessToken $token): VatsimResourceOwner
{
return new VatsimResourceOwner($response['data']);
}
}
Loading

0 comments on commit b8ef340

Please sign in to comment.