RAPP Platform API
 All Classes Namespaces Files Functions Variables Typedefs
RAPPAuth.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 
5 # Copyright 2016 RAPP
6 
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 
11  #http://www.apache.org/licenses/LICENSE-2.0
12 
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 
19 # Authors: Konstantinos Panayiotou
20 # contact: klpanagi@gmail.com
21 
22 
23 ## @file ServiceController/RAPPAuth.py
24 #
25 # @copyright Rapp Projecty EU 2016
26 # @author Konstantinos Panayiotou, [klpanagi@gmail.com]
27 #
28 
29 
30 from requests.auth import AuthBase
31 from os import path
32 
33 
34 class RAPPAuth(AuthBase):
35  """ RAPP Authentication Class
36 
37  Implements the RAPP Authentication mechanism for Platform requests.
38  """
39 
40  def __init__(self, token='', svcname=''):
41  """! Constructor
42 
43  @param string token
44  @param string svcname - The service/application name.
45  """
46  # The application tokens storage directory.
47  self._tokenStoreDir = path.expanduser('~/.config/rapp_platform/tokens')
48 
49  if token is not '':
50  self._token = token
51  else:
52  self.__load_token()
53 
54 
55  def __call__(self, r):
56  """! Append the application token to the HTTP application protocol header.
57 
58  Modify the request object. Append token string to the Accept-Token
59  field of the request header.
60  """
61  r.headers['Accept-Token'] = self._token
62  return r
63 
64 
65  def __load_token(self, svcname=""):
66  """! Load the RAPP Platform authentication token.
67 
68  Currently only one application token exists, giving access to all the
69  RAPP Platform services!!
70  """
71  ## TODO Load different tokens per application request.
72  self.__read_token_from_file(path.join(self._tokenStoreDir, 'app'))
73 
74 
75  def __read_token_from_file(self, filepath):
76  """! Read and token string stored in a file.
77 
78  @param filepath string - The token file path.
79  """
80  with open(filepath, 'r') as tokenF:
81  self._token = str(tokenF.read()).replace('\n', '')
82 
83 
def __read_token_from_file
Read and token string stored in a file.
Definition: RAPPAuth.py:75
def __call__
Append the application token to the HTTP application protocol header.
Definition: RAPPAuth.py:55
def __load_token
Load the RAPP Platform authentication token.
Definition: RAPPAuth.py:65