Passed
Push — master ( 35fb8a...2dccb2 )
by Pieter van der
05:32 queued 14s
created

verifyResponse()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 15
c 0
b 0
f 0
dl 0
loc 22
ccs 0
cts 15
cp 0
rs 9.7666
cc 2
nc 3
nop 5
crap 6
1
<?php
2
3
/**
4
 * This file is part of the tiqr project.
5
 *
6
 * The tiqr project aims to provide an open implementation for
7
 * authentication using mobile devices. It was initiated by
8
 * SURFnet and developed by Egeniq.
9
 *
10
 * More information: http://www.tiqr.org
11
 *
12
 * @author Ivo Jansch <[email protected]>
13
 *
14
 * @package tiqr
15
 *
16
 * @license New BSD License - See LICENSE file for details.
17
 *
18
 * @copyright (C) 2010-2012 SURFnet BV
19
 */
20
21
use Psr\Log\LoggerInterface;
22
23
require_once('Tiqr/API/Client.php');
24
25
/**
26
 * The implementation for the oathservice ocra service class.
27
 *
28
 * @author lineke
29
 *
30
 */
31
class Tiqr_OcraService_OathServiceClient extends Tiqr_OcraService_Abstract
32
{
33
    /** @var Tiqr_API_Client */
34
    protected $_apiClient;
35
36
    /**
37
     * Construct a OCRA service that uses the Tiqr_API_Client to use an OCRA KeyServer
38
     *
39
     * @param array $config The configuration that a specific user class may use.
40
     * @throws Exception
41
     */
42 1
    public function __construct(array $config, LoggerInterface $logger)
43
    {
44 1
        parent::__construct($config, $logger);
45
46 1
        if (!isset($config['apiURL'])) {
47
            throw new RuntimeException('Missing apiURL in config for oathserviceclient');
48
        }
49 1
        if (!isset($config['consumerKey'])) {
50
            throw new RuntimeException('Missing consumerKey in config for oathserviceclient');
51
        }
52
53 1
        $this->_apiClient = new Tiqr_API_Client();
54 1
        $this->_apiClient->setBaseURL($config['apiURL']);
55 1
        $this->_apiClient->setConsumerKey($config['consumerKey']);
56 1
    }
57
58
    // Use the implementation in the abstract class to generate the challenge locally
59
    // public function generateChallenge(): string
60
61
    // Use the implementation in the abstract class to generate the session key (i.e. session information) locally
62
    // public function generateSessionKey(): string
63
64
    // Use a remote server to verify the response
65
    public function verifyResponse(string $response, string $userId, string $userSecret, string $challenge, string $sessionInformation): bool
66
    {
67
        try {
68
            $result = $this->_apiClient->call('/oath/validate/ocra?response='.urlencode($response).'&challenge='.urlencode($challenge).'&userId='.urlencode($userId).'&sessionKey='.urlencode($sessionInformation));
69
            $this->logger->notice(
70
                sprintf(
71
                    'Verify response api call returned status code %s and response body: %s.',
72
                    $result->code,
73
                    $result->body
74
                )
75
            );
76
            // Tiqr_API_Client::call throws when it gets a non HTTP 2xx response
77
            return true;
78
        } catch (Exception $e) {
79
            $this->logger->error(
80
                sprintf(
81
                    'verifyResponse for user "%s" failed',
82
                    $userId
83
                ),
84
                array( 'exception' => $e)
85
            );
86
            return false;
87
        }
88
    }
89
90
}
91