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
use Exception;
6
 
7
/**
8
 * Class EduSharingHelper
9
 *
10
 * @author Torsten Simon  <simon@edu-sharing.net>
11
 */
12
class EduSharingHelper
13
{
14
    /**
15
     * Function generateKeyPair
16
     *
17
     * generate a new key pair (private + public) to be registered in the edu-sharing repository
18
     * Store the data somewhere in your application, e.g. database
19
     * use the public key returned to register the application in edu-sharing
20
     * NOTE: This function will fail on windows-based systems!
21
     * @throws Exception
22
     */
23
    public static function generateKeyPair(): array {
24
        $res = openssl_pkey_new();
25
        !$res && throw new Exception('No result from openssl_pkey_new. Please check your php installation');
26
        openssl_pkey_export($res, $privateKey);
27
        $publicKeyData = openssl_pkey_get_details($res);
28
        $publicKey     = $publicKeyData['key'];
29
        return [
30
            'privateKey' => $privateKey,
31
            'publicKey'  => $publicKey
32
        ];
33
    }
34
 
35
    /**
36
     * Function generateEduAppXMLData
37
     *
38
     * Generates an edu-sharing compatible xml file for registering the application
39
     * This is a very basic function and is only intended for demonstration or manual use. Data is not escaped!
40
     */
41
    public static function generateEduAppXMLData(string $appId, string $publicKey, string $type = 'LMS', string $publicIP = '*'): string {
42
        return '<?xml version="1.0" encoding="UTF-8"?>
43
                <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
44
                <properties>
45
                    <entry key="appid">' . $appId . '</entry>
46
                    <entry key="public_key">' . $publicKey . '</entry>
47
                    <entry key="type">' . $type . '</entry>
48
                    <entry key="domain"></entry>
49
                    <!-- in case of wildcard host: Replace this, if possible, with the public ip from your service -->
50
                    <entry key ="host">' . $publicIP . '</entry>
51
                    <!-- must be true -->
52
                    <entry key="trustedclient">true</entry>
53
                </properties>
54
                ';
55
    }
56
}
57