blob: 851a79d64807f7f90e9a66829b01ba5219a72111 [file] [log] [blame]
srikanth116e6e82014-08-19 07:22:37 -07001#
2# Copyright (c) 2013 Big Switch Networks, Inc.
3#
4# Licensed under the Eclipse Public License, Version 1.0 (the
5# "License"); you may not use this file except in compliance with the
6# License. You may obtain a copy of the License at
7#
8# http://www.eclipse.org/legal/epl-v10.html
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13# implied. See the License for the specific language governing
14# permissions and limitations under the License.
15#
16
17# Create your views here.
18
19from django.contrib.auth.decorators import login_required
20from django.utils import simplejson
21from django.http import HttpResponse
22from sdncon.clusterAdmin.models import CustomerUser
23from sdncon.clusterAdmin.utils import conditionally, isCloudBuild
24from sdncon.rest.views import safe_rest_view
25
26DEFAULT_TENANT = 'default'
27JSON_DATA_TYPE = 'application/json'
28
29@safe_rest_view
30def get_node_personality(request):
31 isCloud = isCloudBuild()
32 response_data = {'cloud' : isCloud, 'controller-node' : not isCloud}
33 response_data = simplejson.dumps(response_data)
34 return HttpResponse(response_data, JSON_DATA_TYPE)
35
36@conditionally(login_required, isCloudBuild())
37@safe_rest_view
38def session_clusterAdmin(request):
39 """
40 This returns the customer and clusters, which the current session is associated with.
41 """
42 customer = {}
43 customer['user'] = request.user.username
44 customer['customer'] = DEFAULT_TENANT
45 customer['cluster'] = []
46 if request.user.is_authenticated():
47 cus = CustomerUser.objects.all()
48 for cu in cus:
49 if cu.user.username == request.user.username:
50 customer['customer'] = cu.customer.customername
51 for cluster in cu.customer.cluster_set.all():
52 customer['cluster'].append(cluster.clustername)
53
54 response_data = simplejson.dumps(customer)
55 return HttpResponse(response_data, JSON_DATA_TYPE)
56
57
58import random
59from django.shortcuts import render_to_response, redirect
60from .models import AuthToken
61
62@conditionally(login_required, isCloudBuild())
63def token_view(request):
64 token=''
65 for t in AuthToken.objects.filter(user=request.user):
66 token=t.id
67 return render_to_response('registration/token_view.html', {'token': token})
68
69################################################################
70
71VALID_TOKEN_CHARS = ['3', '4', '6', '7', '9', 'A', 'C', 'E', 'F', 'G',
72 'H', 'K', 'M', 'N', 'P', 'R', 'T', 'W', 'X', 'Y']
73TOKEN_LENGTH = 16
74TOKEN_GEN_ATTEMPTS = 16 # Number of times generated token can be in DB before giving up
75
76#
77# Token Specification
78# 16 characters, hyphens every 4 characters
79# ....-....-....-....
80# Key is actually this string, including the -s
81# Characters used must be in the VALID_TOKEN_CHARS array above
82# This gives a token space of 20^16
83#
84
85@conditionally(login_required, isCloudBuild())
86def token_generate(request):
87 """
88 Generate, validate and add an auth token
89 """
90
91 for try_count in range(TOKEN_GEN_ATTEMPTS):
92 token_string = ""
93 for i in range(TOKEN_LENGTH):
94 if i > 0 and i % 4 == 0:
95 token_string += '-'
96 token_string += random.choice(VALID_TOKEN_CHARS)
97 print "Generated token " + token_string
98
99 # See if the token already exists in the DB; race condition here, but should be minimal
100 try:
101 AuthToken.objects.get(id=token_string)
102 print "Token already present in DB, try " + str(try_count)
103 token_string = ""
104 except AuthToken.DoesNotExist: # This is what we want
105 break
106
107 if not token_string:
108 # Should probably raise an exception here
109 print "Failed to generate new auth token"
110 # Return internal server error
111 return
112
113 # Map the token to a customer or cluster
114 # For now, just map one token to the user
115
116 # Delete any existing tokens
117 for t in AuthToken.objects.filter(user=request.user):
118 print 'Deleting token', t
119 t.delete()
120
121 # Add the new token to the DB
122 token = AuthToken(id=token_string, user=request.user)
123 token.save()
124
125 return redirect('sdncon.clusterAdmin.views.token_view')