Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Workflow
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
2 / 2
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 consumerCanBeAutoApproved
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2
3namespace MediaWiki\Extension\OAuth\Control;
4
5use MediaWiki\Config\ServiceOptions;
6use MediaWiki\Extension\OAuth\Backend\Consumer;
7
8/** Service class for OAuth workflow-related business logic. */
9class Workflow {
10
11    /** @internal For use by ServiceWiring */
12    public const CONSTRUCTOR_OPTIONS = [
13        'OAuthAutoApprove',
14    ];
15
16    public const AUTOAPPROVE_RULE_GRANTS = 'grants';
17
18    public function __construct(
19        private readonly ServiceOptions $options,
20    ) {
21        $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
22    }
23
24    /**
25     * True if this is a low-risk consumer that does not require manual approval from an
26     * OAuth admin, and can go straight to the 'approved' stage after creation.
27     * @param Consumer $consumer
28     * @return bool
29     */
30    public function consumerCanBeAutoApproved( Consumer $consumer ): bool {
31        foreach ( $this->options->get( 'OAuthAutoApprove' ) as $condition ) {
32            // check 'grants' rule
33            if ( array_key_exists( self::AUTOAPPROVE_RULE_GRANTS, $condition ) ) {
34                $allowedGrants = $condition[self::AUTOAPPROVE_RULE_GRANTS];
35                if ( array_diff( $consumer->getGrants(), $allowedGrants ) !== [] ) {
36                    continue;
37                }
38                unset( $condition[self::AUTOAPPROVE_RULE_GRANTS] );
39            }
40
41            // check for unsupported rules
42            if ( $condition ) {
43                continue;
44            }
45
46            return true;
47        }
48        // none of the conditions matched
49        return false;
50    }
51
52}