aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/api/oauth.php
blob: 22caab1bc7fbcbb19544c3acae6004069ebdbf88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php

require_once 'lib/bootstrap-api.php';

/**
 * @deprecated Since Stud.IP 5.0. Will be removed in Stud.IP 5.2.
 **/
class Api_OauthController extends StudipController
{
    /**
     *
     **/
    public function before_filter(&$action, &$args)
    {
        parent::before_filter($action, $args);

        # initialize Stud.IP-Session
        page_open(['sess' => 'Seminar_Session',
                        'auth' => 'Seminar_Default_Auth',
                        'perm' => 'Seminar_Perm',
                        'user' => 'Seminar_User']);

        $this->set_layout(null);
    }

    /**
     *
     **/
    public function index_action()
    {
        $this->render_text('TODO');
    }

    /**
     *
     **/
    public function request_token_action()
    {
        try {
            $server = new OAuthServer();
            $token = $server->requestToken();
            $this->render_nothing();
        } catch (Exception $e) {
            $this->render_text($e->getMessage());
        }
    }

    /**
     *
     **/
    public function authorize_action()
    {
        global $user, $auth;

        $auth_plugin = Config::get()->API_OAUTH_AUTH_PLUGIN;
        if ($GLOBALS['user']->id === 'nobody' && $auth_plugin !== 'Standard' && !Request::option('sso')) {
            $params = $_GET;
            $params['sso'] = strtolower($auth_plugin);
            $this->redirect($this->url_for('api/oauth/authorize?' . http_build_query($params)));
            return;
        } else {
            $auth->login_if($user->id === 'nobody');
        }

        $user_id = RESTAPI\Consumer\OAuth::getOAuthId($GLOBALS['user']->id);

        try {
            $consumer = RESTAPI\Consumer\Base::detectConsumer('oauth', 'request');
            if (!$consumer) {
                $this->response->set_status(400, 'No consumer detected');
                $this->render_nothing();
                return;
            }

            if (Request::submitted('allow')) {
                $result = $consumer->grantAccess($GLOBALS['user']->id);

                $redirect_uri = Request::get('oauth_callback', $consumer->callback);

                if ($redirect_uri) {
                    $this->redirect($redirect_uri);
                } else {
                    // No oauth_callback, show the user the result of the authorization
                    // ** your code here **
                    PageLayout::postMessage(MessageBox::success(_('Sie haben der Applikation Zugriff auf Ihre Daten gewährt.')));
                    $this->redirect('api/authorizations#' . $consumer->auth_key);
                }
                return;
           }
        } catch (OAuthException2 $e) {
            // No token to be verified in the request, show a page where the user can enter the token to be verified
            // **your code here**
            die('invalid');
        }

        PageLayout::disableHeader();
        PageLayout::setTitle(sprintf(_('"%s" bittet um Zugriff'), $consumer->title));
        $this->set_layout($GLOBALS['template_factory']->open('layouts/base.php'));
        $this->consumer = $consumer;
        $this->token    = Request::option('oauth_token');
        $this->oauth_callback = Request::get('oauth_callback');
    }

    /**
     *
     **/
    public function access_token_action()
    {
        $server = new OAuthServer();
        $server->accessToken();

        $this->render_nothing();
    }
}