Tiqr_OcraService_OathServiceClient::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0987

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 14
ccs 7
cts 9
cp 0.7778
rs 10
cc 3
nc 3
nop 2
crap 3.0987
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
/**
24
 * The implementation for the oathservice ocra service class.
25
 *
26
 * @author lineke
27
 *
28
 */
29
class Tiqr_OcraService_OathServiceClient extends Tiqr_OcraService_Abstract
30
{
31
    /** @var Tiqr_API_Client */
32
    protected $_apiClient;
33
34
    /**
35
     * Construct a OCRA service that uses the Tiqr_API_Client to use an OCRA KeyServer
36
     *
37
     * @param array $config The configuration that a specific user class may use.
38
     * @throws Exception
39
     */
40 1
    public function __construct(array $config, LoggerInterface $logger)
41
    {
42 1
        parent::__construct($config, $logger);
43
44 1
        if (!isset($config['apiURL'])) {
45
            throw new RuntimeException('Missing apiURL in config for oathserviceclient');
46
        }
47 1
        if (!isset($config['consumerKey'])) {
48
            throw new RuntimeException('Missing consumerKey in config for oathserviceclient');
49
        }
50
51 1
        $this->_apiClient = new Tiqr_API_Client();
52 1
        $this->_apiClient->setBaseURL($config['apiURL']);
53 1
        $this->_apiClient->setConsumerKey($config['consumerKey']);
54
    }
55
56
    // Use the implementation in the abstract class to generate the challenge locally
57
    // public function generateChallenge(): string
58
59
    // Use the implementation in the abstract class to generate the session key (i.e. session information) locally
60
    // public function generateSessionKey(): string
61
62
    // Use a remote server to verify the response
63
    public function verifyResponse(string $response, string $userId, string $userSecret, string $challenge, string $sessionInformation): bool
64
    {
65
        try {
66
            $result = $this->_apiClient->call('/oath/validate/ocra?response='.urlencode($response).'&challenge='.urlencode($challenge).'&userId='.urlencode($userId).'&sessionKey='.urlencode($sessionInformation));
67
            $this->logger->notice(
68
                sprintf(
69
                    'Verify response api call returned status code %s and response body: %s.',
70
                    $result->code,
71
                    $result->body
72
                )
73
            );
74
            // Tiqr_API_Client::call throws when it gets a non HTTP 2xx response
75
            return true;
76
        } catch (Exception $e) {
77
            $this->logger->error(
78
                sprintf(
79
                    'verifyResponse for user "%s" failed',
80
                    $userId
81
                ),
82
                array( 'exception' => $e)
83
            );
84
            return false;
85
        }
86
    }
87
88
}
89