cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
auth.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 # @COPYRIGHT_begin
3 #
4 # Copyright [2010-2014] Institute of Nuclear Physics PAN, Krakow, Poland
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # @COPYRIGHT_end
19 
20 ##
21 # @package src.helpers.s3action
22 # Decorator for authenticating S3 requests
23 #
24 # @copyright Copyright (c) 2012 Institute of Nuclear Physics PAS <http://www.ifj.edu.pl/>
25 # @author Łukasz Chrząszcz <l.chrzaszcz@gmail.com>
26 #
27 import os
28 from common.utils import ServerProxy
29 from ec2.base.action import CLMException
30 from ec2.error import AuthFailure, InvalidAccessKeyId
31 from ec2.settings import CLM_ADDRESS, BUCKETS_PATH
32 
33 CLM_AUTHENTICATE = '/guest/user/check_signature/'
34 
35 
36 def authenticate(parameters):
37  clm = ServerProxy(CLM_ADDRESS)
38 
39  temp_input = parameters['input']
40  temp_file_wrapper = parameters['file_wrapper']
41 
42  del parameters['input']
43  del parameters['file_wrapper']
44 
45  data = {'parameters': parameters}
46  response = clm.send_request(CLM_AUTHENTICATE, **data)
47  status = response['status']
48 
49  print response
50 
51  if status != 'ok':
52  raise AuthFailure()
53 
54  parameters['input'] = temp_input
55  parameters['file_wrapper'] = temp_file_wrapper
56 
57  user_name = parameters['authorization'].split(' ')[1].split(':')[0]
58  print 'Username:', user_name
59  if user_name.find('..') != -1:
60  raise InvalidAccessKeyId
61 
62  bucket_path = os.path.join(BUCKETS_PATH, user_name)
63  print 'Bucket path:', bucket_path
64 
65  if not os.path.exists(bucket_path):
66  print 'User\'s directory does not exists, creating'
67  os.mkdir(bucket_path)
68 
69  print 'Authentication process successful'
70  return True
71