Passed
Pull Request — develop (#27)
by Michiel
06:20
created

Tiqr_OcraService_OathServiceClient   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 18.18%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 32
c 1
b 0
f 0
dl 0
loc 85
ccs 6
cts 33
cp 0.1818
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A generateChallenge() 0 15 2
A verifyResponseWithUserId() 0 20 2
A getVerificationMethodName() 0 3 1
A __construct() 0 6 1
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 1
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
    protected $_apiClient;
34
35
    /**
36
     * @var LoggerInterface
37
     */
38
    private $logger;
39
40
    /**
41
     * Construct a ocra service class
42
     *
43
     * @param array $config The configuration that a specific user class may use.
44
     */
45 1
    public function __construct($config, LoggerInterface $logger)
46
    {
47 1
        $this->_apiClient = new Tiqr_API_Client();
48 1
        $this->_apiClient->setBaseURL($config['apiURL']);
49 1
        $this->_apiClient->setConsumerKey($config['consumerKey']);
50 1
        $this->logger = $logger;
51 1
    }
52
53
    /**
54
     * Get the ocra challenge
55
     *
56
     * @return string|null The challenge or null when no challenge was returned form the Oath service
57
     */
58
    public function generateChallenge()
59
    {
60
        $result = $this->_apiClient->call('/oath/challenge/ocra');
61
        if ($result->code == '200') {
62
            $this->logger->notice(
63
                sprintf(
64
                    'Challenge api call returned status code %s and response body: %s.',
65
                    $result->code,
66
                    $result->body
67
                )
68
            );
69
            return $result->body;
70
        }
71
        $this->logger->error('The call to /oath/challenge/ocra did not yield a challenge.');
72
        return null;
73
    }
74
75
    /**
76
     * Verify the response
77
     *
78
     * @param string $response
79
     * @param string $userId
80
     * @param string $challenge
81
     * @param string $sessionKey
82
     *
83
     * @return boolean True if response matches, false otherwise
84
     */
85
    public function verifyResponseWithUserId($response, $userId, $challenge, $sessionKey)
86
    {
87
        try {
88
            $result = $this->_apiClient->call('/oath/validate/ocra?response='.urlencode($response).'&challenge='.urlencode($challenge).'&userId='.urlencode($userId).'&sessionKey='.urlencode($sessionKey));
89
            $this->logger->notice(
90
                sprintf(
91
                    'Verify response api call returned status code %s and response body: %s.',
92
                    $result->code,
93
                    $result->body
94
                )
95
            );
96
            return true;
97
        } catch (Exception $e) {
98
            $this->logger->error(
99
                sprintf(
100
                    'Calling of verifyResponseWithUserId failed with message: "%s"',
101
                    $e->getMessage()
102
                )
103
            );
104
            return false;
105
        }
106
    }
107
108
    /**
109
     * Returns which method name to use to verify the response (verifyResponseWithSecret or verifyResponseWithUserId)
110
     *
111
     * @return string
112
     */
113
    public function getVerificationMethodName()
114
    {
115
        return 'verifyResponseWithUserId';
116
    }
117
}
118