GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — release-1.x ( a5e082...76423c )
by Boy
05:57 queued 02:54
created

YubikeyOtp::fromString()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 24
rs 8.6845
cc 4
eloc 17
nc 4
nop 1
1
<?php
2
3
/**
4
 * Copyright 2014 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupBundle\Value;
20
21
use Surfnet\StepupBundle\Exception\InvalidArgumentException;
22
23
class YubikeyOtp
24
{
25
    const OTP_REGEXP_QWERTY = '/^((.*):)?(([cbdefghijklnrtuv]{0,16})([cbdefghijklnrtuv]{32}))$/i';
26
    const OTP_REGEXP_DVORAK = '/^((.*):)?(([jxe\.uidchtnbpygk]{0,16})([jxe\.uidchtnbpygk]{32}))$/i';
27
28
    /** @var string */
29
    public $otp;
30
31
    /** @var string */
32
    public $password;
33
34
    /** @var string */
35
    public $publicId;
36
37
    /** @var string */
38
    public $cipherText;
39
40
    /**
41
     * @param string $string
42
     * @return YubikeyOtp
43
     * @throws InvalidArgumentException Thrown when the given string is not an OTP.
44
     */
45
    public static function fromString($string)
46
    {
47
        $otp = new self;
48
49
        if (!is_string($string)) {
50
            throw new InvalidArgumentException('Given OTP is not a string.');
51
        }
52
53
        if (preg_match(self::OTP_REGEXP_QWERTY, $string, $matches)) {
54
            $otp->otp = strtolower($matches[3]);
55
            $otp->password = $matches[2];
56
            $otp->publicId = strtolower($matches[4]);
57
            $otp->cipherText = strtolower($matches[5]);
58
        } elseif (preg_match(self::OTP_REGEXP_DVORAK, $string, $matches)) {
59
            $otp->otp = strtr(strtolower($matches[3]), 'jxe.uidchtnbpygk', 'cbdefghijklnrtuv');
60
            $otp->password = $matches[2];
61
            $otp->publicId = strtr(strtolower($matches[4]), 'jxe.uidchtnbpygk', 'cbdefghijklnrtuv');
62
            $otp->cipherText = strtr(strtolower($matches[5]), 'jxe.uidchtnbpygk', 'cbdefghijklnrtuv');
63
        } else {
64
            throw new InvalidArgumentException('Given string is not a valid OTP.');
65
        }
66
67
        return $otp;
68
    }
69
70
    /**
71
     * @param string $string
72
     * @return bool
73
     */
74
    public static function isValid($string)
75
    {
76
        return preg_match(self::OTP_REGEXP_QWERTY, $string, $matches)
77
            || preg_match(self::OTP_REGEXP_DVORAK, $string, $matches);
78
    }
79
}
80