Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.74% covered (success)
94.74%
18 / 19
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Workflow
94.74% covered (success)
94.74%
18 / 19
50.00% covered (danger)
50.00%
1 / 2
10.01
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 consumerCanBeAutoApproved
94.44% covered (success)
94.44%
17 / 18
0.00% covered (danger)
0.00%
0 / 1
9.01
1<?php
2
3namespace MediaWiki\Extension\OAuth\Control;
4
5use MediaWiki\Config\ServiceOptions;
6use MediaWiki\Extension\OAuth\Backend\Consumer;
7use MediaWiki\Utils\UrlUtils;
8
9/** Service class for OAuth workflow-related business logic. */
10class Workflow {
11
12    /** @internal For use by ServiceWiring */
13    public const CONSTRUCTOR_OPTIONS = [
14        'OAuthAutoApprove',
15    ];
16
17    public const AUTOAPPROVE_RULE_GRANTS = 'grants';
18    public const AUTOAPPROVE_RULE_PROTOCOLS = 'protocols';
19
20    public function __construct(
21        private readonly ServiceOptions $options,
22        private readonly UrlUtils $oauthUrlUtils,
23    ) {
24        $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
25    }
26
27    /**
28     * True if this is a low-risk consumer that does not require manual approval from an
29     * OAuth admin, and can go straight to the 'approved' stage after creation.
30     * @param Consumer $consumer
31     * @return bool
32     */
33    public function consumerCanBeAutoApproved( Consumer $consumer ): bool {
34        $urlParts = $this->oauthUrlUtils->parse( $consumer->getCallbackUrl() );
35        if ( ( ( $urlParts['port'] ?? null ) === 1 ) && $consumer->getCallbackIsPrefix() ) {
36            // wildcard port
37            return false;
38        }
39
40        foreach ( $this->options->get( 'OAuthAutoApprove' ) as $condition ) {
41            // check 'grants' rule
42            if ( array_key_exists( self::AUTOAPPROVE_RULE_GRANTS, $condition ) ) {
43                $allowedGrants = $condition[self::AUTOAPPROVE_RULE_GRANTS];
44                if ( array_diff( $consumer->getGrants(), $allowedGrants ) !== [] ) {
45                    continue;
46                }
47                unset( $condition[self::AUTOAPPROVE_RULE_GRANTS] );
48            }
49
50            // check 'protocols' rule
51            if ( array_key_exists( self::AUTOAPPROVE_RULE_PROTOCOLS, $condition ) ) {
52                $allowedProtocols = $condition[self::AUTOAPPROVE_RULE_PROTOCOLS];
53                if ( !in_array( $urlParts['scheme'] ?? null, $allowedProtocols, true ) ) {
54                    continue;
55                }
56                unset( $condition[self::AUTOAPPROVE_RULE_PROTOCOLS] );
57            }
58
59            // check for unsupported rules
60            if ( $condition ) {
61                continue;
62            }
63
64            return true;
65        }
66        // some of the conditions didn't match
67        return false;
68    }
69
70}