Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php declare(strict_types=1);
2
 
3
namespace EduSharingApiClient;
4
 
5
/**
6
 * Class EduSharingHelperAbstract
7
 *
8
 * @author Torsten Simon  <simon@edu-sharing.net>
9
 */
10
abstract class EduSharingHelperAbstract
11
{
12
    public EduSharingHelperBase $base;
13
 
14
    /**
15
     * EduSharingHelperAbstract constructor
16
     *
17
     * @param EduSharingHelperBase $base
18
     */
19
    public function __construct(EduSharingHelperBase $base) {
20
        $this->base = $base;
21
    }
22
 
23
    /**
24
     * Function getRESTAuthenticationHeader
25
     *
26
     * Generates the header to use for a given ticket to authenticate with any edu-sharing api endpoint
27
     * @param string $ticket
28
     * The ticket, obtained by @getTicketForUser
29
     * @return string
30
     */
31
    public function getRESTAuthenticationHeader(string $ticket): string {
32
        return 'Authorization: EDU-TICKET ' . $ticket;
33
    }
34
 
35
    /**
36
     * Function getSignatureHeaders
37
     *
38
     * @param string $signString
39
     * @param string $accept
40
     * @param string $contentType
41
     * @return string[]
42
     */
43
    protected function getSignatureHeaders(string $signString, string $accept = 'application/json', string $contentType = 'application/json'): array {
44
        $ts        = time() * 1000;
45
        $toSign    = $this->base->appId . $signString . $ts;
46
        $signature = $this->sign($toSign);
47
        return [
48
            'Accept: ' . $accept,
49
            'Content-Type: ' . $contentType,
50
            'X-Edu-App-Id: ' . $this->base->appId,
51
            'X-Edu-App-Signed: ' . $toSign,
52
            'X-Edu-App-Sig: ' . $signature,
53
            'X-Edu-App-Ts: ' . $ts,
54
        ];
55
    }
56
 
57
    /**
58
     * Function sign
59
     *
60
     * @param string $toSign
61
     * @return string
62
     */
63
    protected function sign(string $toSign): string {
64
        return $this->base->sign($toSign);
65
    }
66
}