Class: PritunlApiClient::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/pritunl_api_client/key.rb

Overview

Interact with /key api’s

Instance Method Summary collapse

Constructor Details

#initialize(api) ⇒ Key

Returns a new instance of Key.

Parameters:



7
8
9
# File 'lib/pritunl_api_client/key.rb', line 7

def initialize( api )
  @api = api
end

Instance Method Details

#download(organization_id:, user_id:, path: nil) ⇒ String

Deprecated.

This method of downloading the OVPN file is not reliable. Use #download_tar or #download_zip instead.

Note:

User organization must be attached to a server AND user must be enabled and NOT connected!

Download a users key

Parameters:

  • organization_id (String)
  • user_id (String)
  • path (String) (defaults to: nil)

    Local path to save downloaded file (if omitted, file content fill be returned)

Returns:

  • (String)

    Local path to downloaded file or file contents if ‘path’ was omitted

Raises:

  • (StandardError)

    if user or servers cannot be found



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/pritunl_api_client/key.rb', line 20

def download( organization_id:, user_id:, path: nil )
  temporary_url_id = temporary_url( organization_id: organization_id, user_id: user_id )['id']
  all_users = @api.get( "/user/#{organization_id}" )
  user = all_users.find { |user| user['id'] == user_id }
  fail StandardError, 'Could not find user!' unless user
  servers = user['servers']
  fail StandardError, 'Could not find servers attached to user!' unless servers && servers.count >= 1
  server_id = servers.first['id']
  data = @api.get( "/key/#{temporary_url_id}/#{server_id}.key" )
  return data unless path
  File.write( path, data.force_encoding( 'utf-8' ) )
  path
end

#download_chromebook_profile(organization_id:, user_id:, path: nil) ⇒ String

Download a users onc key (Chromebook profile) as a zip archive

Parameters:

  • organization_id (String)
  • user_id (String)
  • path (String) (defaults to: nil)

    Local path to save downloaded file (if omitted, file content fill be returned)

Returns:

  • (String)

    Local path to downloaded file or file contents if ‘path’ was omitted



66
67
68
69
70
71
# File 'lib/pritunl_api_client/key.rb', line 66

def download_chromebook_profile( organization_id:, user_id:, path: nil )
  data = @api.get( "/key_onc/#{organization_id}/#{user_id}.zip" )
  return data unless path
  File.write( path, data.force_encoding( 'utf-8' ) )
  path
end

#download_tar(organization_id:, user_id:, path: nil) ⇒ String

Download a users key tar archive

Parameters:

  • organization_id (String)
  • user_id (String)
  • path (String) (defaults to: nil)

    Local path to save downloaded file (if omitted, file content fill be returned)

Returns:

  • (String)

    Local path to downloaded file or file contents if ‘path’ was omitted



40
41
42
43
44
45
# File 'lib/pritunl_api_client/key.rb', line 40

def download_tar( organization_id:, user_id:, path: nil )
  data = @api.get( "/key/#{organization_id}/#{user_id}.tar" )
  return data unless path
  File.write( path, data.force_encoding( 'utf-8' ) )
  path
end

#download_zip(organization_id:, user_id:, path: nil) ⇒ String

Download a users key zip archive

Parameters:

  • organization_id (String)
  • user_id (String)
  • path (String) (defaults to: nil)

    Local path to save downloaded file (if omitted, file content fill be returned)

Returns:

  • (String)

    Local path to downloaded file or file contents if ‘path’ was omitted



53
54
55
56
57
58
# File 'lib/pritunl_api_client/key.rb', line 53

def download_zip( organization_id:, user_id:, path: nil )
  data = @api.get( "/key/#{organization_id}/#{user_id}.zip" )
  return data unless path
  File.write( path, data.force_encoding( 'utf-8' ) )
  path
end

#temporary_url(organization_id:, user_id:) ⇒ Hash

Generate a temporary url to download a users key archive

Parameters:

  • organization_id (String)
  • user_id (String)

Returns:

  • (Hash)


78
79
80
# File 'lib/pritunl_api_client/key.rb', line 78

def temporary_url( organization_id:, user_id: )
  @api.get( "/key/#{organization_id}/#{user_id}" )
end