Passed
Pull Request — master (#2)
by Tim
02:52
created

OTP2YubiPrefix   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 31
rs 10
c 0
b 0
f 0
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A process() 0 23 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\authYubikey\Auth\Process;
6
7
use SimpleSAML\Assert\Assert;
8
use SimpleSAML\Auth;
9
use SimpleSAML\Logger;
10
11
/*
12
 * Copyright (C) 2009  Simon Josefsson <[email protected]>.
13
 *
14
 * This file is part of SimpleSAMLphp
15
 *
16
 * SimpleSAMLphp is free software; you can redistribute it and/or
17
 * modify it under the terms of the GNU Lesser General Public License
18
 * as published by the Free Software Foundation; either version 3 of
19
 * the License, or (at your option) any later version.
20
 *
21
 * SimpleSAMLphp is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24
 * Lesser General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Lesser General Public
27
 * License License along with GNU SASL Library; if not, write to the
28
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
29
 * Boston, MA 02110-1301, USA.
30
 *
31
 */
32
33
/**
34
 * A processing filter to replace the 'otp' attribute with an attribute
35
 * 'yubiPrefix' that contains the static YubiKey prefix.
36
 *
37
 * Before:
38
 *   otp=ekhgjhbctrgnubeeklijcibbgjnbtjlffdnjbhjluvur
39
 *
40
 * After:
41
 *   otp undefined
42
 *   yubiPrefix=ekhgjhbctrgn
43
 *
44
 * You use it by adding it as an authentication filter in config.php:
45
 *
46
 *      'authproc.idp' => array(
47
 *    ...
48
 *          90 => 'authYubiKey:OTP2YubiPrefix',
49
 *    ...
50
 *      );
51
 *
52
 */
53
54
class OTP2YubiPrefix extends Auth\ProcessingFilter
55
{
56
    /**
57
     * Filter out YubiKey 'otp' attribute and replace it with
58
     * a 'yubiPrefix' attribute that leaves out the dynamic part.
59
     *
60
     * @param array &$state  The state we should update.
61
     */
62
    public function process(array &$state): void
63
    {
64
        Assert::keyExists($state, 'Attributes');
65
        $attributes = $state['Attributes'];
66
67
        Logger::debug('OTP2YubiPrefix: enter with attributes: ' . implode(',', array_keys($attributes)));
68
69
        $otps = $attributes['otp'];
70
        $otp = $otps['0'];
71
72
        $token_size = 32;
73
        $identity = substr($otp, 0, strlen($otp) - $token_size);
74
75
        $attributes['yubiPrefix'] = [$identity];
76
77
        Logger::info(
78
            'OTP2YubiPrefix: otp: ' . $otp . ' identity: ' . $identity . ' (otp keys: '
79
            . implode(',', array_keys($otps)) . ')'
80
        );
81
82
        unset($attributes['otp']);
83
84
        Logger::debug('OTP2YubiPrefix: leaving with attributes: ' . implode(',', array_keys($attributes)));
85
    }
86
}
87