Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Workflow
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
2 / 2
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
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    private ServiceOptions $options;
19
20    /**
21     * @param ServiceOptions $options
22     */
23    public function __construct( ServiceOptions $options ) {
24        $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
25        $this->options = $options;
26    }
27
28    /**
29     * True if this is a low-risk consumer that does not require manual approval from an
30     * OAuth admin, and can go straight to the 'approved' stage after creation.
31     * @param Consumer $consumer
32     * @return bool
33     */
34    public function consumerCanBeAutoApproved( Consumer $consumer ): bool {
35        foreach ( $this->options->get( 'OAuthAutoApprove' ) as $condition ) {
36            // check 'grants' rule
37            if ( array_key_exists( self::AUTOAPPROVE_RULE_GRANTS, $condition ) ) {
38                $allowedGrants = $condition[self::AUTOAPPROVE_RULE_GRANTS];
39                if ( array_diff( $consumer->getGrants(), $allowedGrants ) !== [] ) {
40                    continue;
41                }
42                unset( $condition[self::AUTOAPPROVE_RULE_GRANTS] );
43            }
44
45            // check for unsupported rules
46            if ( $condition ) {
47                continue;
48            }
49
50            return true;
51        }
52        // none of the conditions matched
53        return false;
54    }
55
56}