Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (5.8 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 user = new snf.auth.AstakosClient(astakos_config);
56
    //  if (!user.get_token() { user.redirect_to_login() };
57
    //  console.log(user.get_username(), user.get_token());
58
    //
59

    
60
    var root = root;
61
    var snf = root.synnefo = root.synnefo || {};
62
    
63
    // init auth namespace
64
    snf.auth = {};
65
    
66
    snf.auth.AstakosClient = function(config) {
67
        this.config = $.extend(this.default_config, config);
68
        this.current_token = undefined;
69
        this.current_username = undefined;
70
        this.skip_redirects = config.skip_redirects === undefined ? false : 
71
                              config.skip_redirects;
72
      
73
        var self = this;
74
        this.updater = window.setInterval(function(){
75
          self.get_token();
76
          self.get_username();
77
        }, 10000);
78
    }
79

    
80
    snf.auth.AstakosClient.prototype.default_config = {
81
            'logout_url': '/im/logout',
82
            'login_url': '/im/login',
83
            'cookie_name': '_pithos2_a',
84
            'logout_callback': function(client) {
85
                client.redirect_to_logout();
86
            }
87
    }
88

    
89
    snf.auth.AstakosClient.prototype.delete_cookie = function() {
90
      if (!this.skip_redirects) {
91
        $.cookie(this.config.cookie_name, null);
92
      }
93
    }
94

    
95
    snf.auth.AstakosClient.prototype.redirect_to_logout = function() {
96
      if (!this.skip_redirects) {
97
        window.location = this.config.logout_url + "?next=";
98
      }
99
    }
100
    
101
    snf.auth.AstakosClient.prototype.redirect_to_login = function() {
102
      if (!this.skip_redirects) {
103
        window.location = this.config.login_url + "?next=" + window.location.toString();
104
      }
105
    }
106

    
107
    // delete cookie and redirect to logout
108
    // cookie removal can be forced by passing true as delete_cookie parameter
109
    snf.auth.AstakosClient.prototype.logout = function(delete_cookie) {
110
        var delete_cookie = delete_cookie == undefined ? false : delete_cookie;
111
        if (delete_cookie) {
112
            this.delete_cookie();
113
        }
114
        this.config.logout_callback(this);
115
    }
116

    
117
    snf.auth.AstakosClient.prototype.get_cookie_data = function() {
118
        var data = $.cookie(this.config.cookie_name);
119
        
120
        // remove " characters
121
        if (data) { return data.replace(/\"/g, "") }
122
        return data;
123
    }
124

    
125

    
126
    snf.auth.AstakosClient.prototype.logged_in = function() {
127
        return this.get_cookie_data() == null
128
    }
129

    
130
    // parse cookie data
131
    // astakos sets cookie data using the following pattern: <username>|<token>
132
    snf.auth.AstakosClient.prototype.parse_cookie_data = function(data) {
133
        return {
134
            'username': data.split("|")[0],
135
            'token': data.split("|")[1]
136
        }
137
    }
138
    
139
    snf.auth.AstakosClient.prototype.extract_cookie_contents = function() {
140
        var data = this.get_cookie_data();
141
        if (!data) {
142
            return {};
143
        }
144
        return this.parse_cookie_data(data);
145
    }
146

    
147
    snf.auth.AstakosClient.prototype.get_token = function() {
148
      var newtoken;
149
      newtoken = this.extract_cookie_contents().token;
150
      if (newtoken === undefined || (newtoken != this.current_token && 
151
          this.current_token != undefined)) {
152
        this.redirect_to_login();
153
      }
154
      this.current_token = newtoken;
155
      return this.current_token;
156
    }
157

    
158
    snf.auth.AstakosClient.prototype.get_username = function() {
159
      var newusername;
160
      newusername = this.extract_cookie_contents().username;
161
      if (newusername === undefined || (newusername != this.current_username && 
162
          this.current_username != undefined)) {
163
        this.redirect_to_login();
164
      }
165
      this.current_username = newusername;
166
      return this.current_username;
167
    }
168
    
169
})(this);