Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / ui / static / snf / js / auth.js @ 4ef604a6

History | View | Annotate | Download (4.7 kB)

1
// Copyright 2012 GRNET S.A. All rights reserved.
2
// 
3
// Redistribution and use in source and binary forms, with or
4
// without modification, are permitted provided that the following
5
// conditions are met:
6
// 
7
//   1. Redistributions of source code must retain the above
8
//      copyright notice, this list of conditions and the following
9
//      disclaimer.
10
// 
11
//   2. Redistributions in binary form must reproduce the above
12
//      copyright notice, this list of conditions and the following
13
//      disclaimer in the documentation and/or other materials
14
//      provided with the distribution.
15
// 
16
// THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
// POSSIBILITY OF SUCH DAMAGE.
28
// 
29
// The views and conclusions contained in the software and
30
// documentation are those of the authors and should not be
31
// interpreted as representing official policies, either expressed
32
// or implied, of GRNET S.A.
33
// 
34

    
35
;(function(root){
36
    
37
    // Astakos client javascript lib
38
    // Requires jquery and jquery.cookie javascript libs
39
    //
40
    // Usage
41
    // -----
42
    // <script src="jquery.js"></script>
43
    // <script src="jquery.cookie.js"></script>
44
    // <script src="snf/auth.js"></script>
45
    //
46
    //  var astakos_config = {
47
    //        'login_url': '/im/login',
48
    //        'auth_url': '/im/authenticate',
49
    //        'cookie_name': '_pithos2_a',
50
    //        'logout_callback': function(client) {
51
    //            console.log("logging out");
52
    //            client.redirect_to_logout();
53
    //        }
54
    //
55
    //  var astakos_client = new snf.auth.AstakosClient(astakos_config);
56
    //  var user = astakos_client.get_user();
57
    //  if (!user) { astakos_client.redirect_to_login() };
58
    //  console.log(user.username, user.token);
59
    //
60

    
61
    var root = root;
62
    var snf = root.synnefo = root.synnefo || {};
63
    
64
    // init auth namespace
65
    snf.auth = {};
66
    
67
    snf.auth.AstakosClient = function(config) {
68
        this.config = $.extend(this.default_config, config);
69
    }
70

    
71
    snf.auth.AstakosClient.prototype.default_config = {
72
            'logout_url': '/im/logout',
73
            'login_url': '/im/login',
74
            'cookie_name': '_pithos2_a',
75
            'logout_callback': function(client) {
76
                client.redirect_to_logout();
77
            }
78
    }
79

    
80
    snf.auth.AstakosClient.prototype.delete_cookie = function() {
81
        $.cookie(this.config.cookie_name, null);
82
    }
83

    
84
    snf.auth.AstakosClient.prototype.redirect_to_logout = function() {
85
        Backbone.history.navigate(this.config.logout_url); 
86
        window.location = this.config.logout_url + "?next=";
87
    }
88
    
89
    snf.auth.AstakosClient.prototype.redirect_to_login = function() {
90
        window.location = this.config.login_url + "?next=" + window.location.toString();
91
    }
92

    
93
    // delete cookie and redirect to logout
94
    // cookie removal can be forced by passing true as delete_cookie parameter
95
    snf.auth.AstakosClient.prototype.logout = function(delete_cookie) {
96
        var delete_cookie = delete_cookie == undefined ? false : delete_cookie;
97
        if (delete_cookie) {
98
            this.delete_cookie();
99
        }
100
        this.config.logout_callback(this);
101
    }
102

    
103
    snf.auth.AstakosClient.prototype.get_cookie_data = function() {
104
        var data = $.cookie(this.config.cookie_name);
105
        
106
        // remove " characters
107
        if (data) { return data.replace(/\"/g, "") }
108
        return data;
109
    }
110

    
111

    
112
    snf.auth.AstakosClient.prototype.logged_in = function() {
113
        return this.get_cookie_data() == null
114
    }
115

    
116
    // parse cookie data
117
    // astakos sets cookie data using the following pattern: <username>|<token>
118
    snf.auth.AstakosClient.prototype.parse_cookie_data = function(data) {
119
        return {
120
            'username': data.split("|")[0],
121
            'token': data.split("|")[1]
122
        }
123
    }
124
    
125
    // set username and token
126
    snf.auth.AstakosClient.prototype.get_user = function() {
127
        var data = this.get_cookie_data();
128
        if (!data) {
129
            return false;
130
        }
131
        var parsed_data = this.parse_cookie_data(data);
132
        return parsed_data;
133
    }
134
    
135
})(this);