Package osgeo :: Package utils :: Module gdal_auth
[hide private]
[frames] | no frames]

Source Code for Module osgeo.utils.gdal_auth

  1  #!/usr/bin/env python3 
  2  # -*- coding: utf-8 -*- 
  3  # ****************************************************************************** 
  4  #  $Id$ 
  5  # 
  6  #  Project:  GDAL 
  7  #  Purpose:  Application for Google web service authentication. 
  8  #  Author:   Frank Warmerdam, warmerdam@pobox.com 
  9  # 
 10  # ****************************************************************************** 
 11  #  Copyright (c) 2013, Frank Warmerdam <warmerdam@pobox.com> 
 12  # 
 13  #  Permission is hereby granted, free of charge, to any person obtaining a 
 14  #  copy of this software and associated documentation files (the "Software"), 
 15  #  to deal in the Software without restriction, including without limitation 
 16  #  the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 17  #  and/or sell copies of the Software, and to permit persons to whom the 
 18  #  Software is furnished to do so, subject to the following conditions: 
 19  # 
 20  #  The above copyright notice and this permission notice shall be included 
 21  #  in all copies or substantial portions of the Software. 
 22  # 
 23  #  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
 24  #  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 25  #  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 26  #  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 27  #  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 28  #  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 29  #  DEALINGS IN THE SOFTWARE. 
 30  # ****************************************************************************** 
 31   
 32   
 33  import sys 
 34  import time 
 35  import webbrowser 
 36   
 37  from osgeo import gdal 
 38   
 39  SCOPES = { 
 40      'ft': 'https://www.googleapis.com/auth/fusiontables', 
 41      'storage': 'https://www.googleapis.com/auth/devstorage.read_only', 
 42      'storage-rw': 'https://www.googleapis.com/auth/devstorage.read_write' 
 43  } 
 44   
 45  # ============================================================================= 
 46  #       Usage() 
 47  # ============================================================================= 
 48   
 49   
50 -def Usage():
51 print('') 52 print('Usage: gdal_auth_py [-s scope]') 53 print(' - interactive use.') 54 print('') 55 print('or:') 56 print('Usage: gdal_auth.py login [-s scope] ') 57 print('Usage: gdal_auth.py auth2refresh [-s scope] auth_token') 58 print('Usage: gdal_auth.py refresh2access [-s scope] refresh_token') 59 print('') 60 print('scopes: ft/storage/storage-rw/full_url') 61 print('') 62 sys.exit(1)
63 64
65 -def main(argv):
66 scope = SCOPES['ft'] 67 token_in = None 68 command = None 69 70 argv = gdal.GeneralCmdLineProcessor(sys.argv) 71 if argv is None: 72 sys.exit(0) 73 74 # Parse command line arguments. 75 i = 1 76 while i < len(argv): 77 arg = argv[i] 78 79 if arg == '-s' and i < len(argv) - 1: 80 if argv[i + 1] in SCOPES: 81 scope = SCOPES[argv[i + 1]] 82 elif argv[i + 1].startswith('http'): 83 scope = argv[i + 1] 84 else: 85 print('Scope %s not recognised.' % argv[i + 1]) 86 Usage() 87 sys.exit(1) 88 i = i + 1 89 90 elif arg[0] == '-': 91 Usage() 92 93 elif command is None: 94 command = arg 95 96 elif token_in is None: 97 token_in = arg 98 99 else: 100 Usage() 101 102 i = i + 1 103 104 if command is None: 105 command = 'interactive' 106 107 if command == 'login': 108 print(gdal.GOA2GetAuthorizationURL(scope)) 109 elif command == 'auth2refresh': 110 print(gdal.GOA2GetRefreshToken(token_in, scope)) 111 elif command == 'refresh2access': 112 print(gdal.GOA2GetAccessToken(token_in, scope)) 113 elif command != 'interactive': 114 Usage() 115 else: 116 # Interactive case 117 print('Authorization requested for scope:') 118 print(scope) 119 print('') 120 print('Please login and authorize access in web browser...') 121 122 webbrowser.open(gdal.GOA2GetAuthorizationURL(scope)) 123 124 time.sleep(2.0) 125 126 print('') 127 print('Enter authorization token:') 128 auth_token = sys.stdin.readline() 129 130 refresh_token = gdal.GOA2GetRefreshToken(auth_token, scope) 131 132 print('Refresh Token:' + refresh_token) 133 print('') 134 if scope == SCOPES['ft']: 135 print('Consider setting a configuration option like:') 136 print('GFT_REFRESH_TOKEN=' + refresh_token) 137 elif scope in (SCOPES['storage'], SCOPES['storage-rw']): 138 print('Consider setting a configuration option like:') 139 print('GS_OAUTH2_REFRESH_TOKEN=' + refresh_token)
140 141 142 if __name__ == '__main__': 143 sys.exit(main(sys.argv)) 144