-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
53d1f74
commit f740a91
Showing
31 changed files
with
475 additions
and
239 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ ratings: | |
- "**.rb" | ||
exclude_paths: | ||
- spec/ | ||
- lib/cli/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
module Fabricio | ||
module Authorization | ||
# A class providing an interface for implementing Fabric session storage. Subclass it to provide your own behaviour (e.g. storing session data in database) | ||
class AbstractParamStorage | ||
|
||
# Returns all stored variable | ||
# | ||
# @return [Hash] | ||
def obtain | ||
raise NotImplementedError, "Implement this method in a child class" | ||
end | ||
|
||
# Save variable | ||
# | ||
# @param hash [Hash] | ||
def store(hash) | ||
raise NotImplementedError, "Implement this method in a child class" | ||
end | ||
|
||
# Resets current state and deletes all saved params | ||
def reset_all | ||
raise NotImplementedError, "Implement this method in a child class" | ||
end | ||
|
||
def organization_id | ||
raise NotImplementedError, "Implement this method in a child class" | ||
end | ||
|
||
def app_id | ||
raise NotImplementedError, "Implement this method in a child class" | ||
end | ||
|
||
def store_organization_id(organization_id) | ||
raise NotImplementedError, "Implement this method in a child class" | ||
end | ||
|
||
def store_app_id(app_id) | ||
raise NotImplementedError, "Implement this method in a child class" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
require 'fabricio/authorization/abstract_param_storage' | ||
require 'fileutils' | ||
require 'yaml' | ||
|
||
# Constants | ||
FABRICIO_DIRECTORY_PATH = "#{Dir.home}/.fabricio" | ||
PARAM_FILE_PATH = "#{CREDENTIAL_DIRECTORY_PATH}/.params" | ||
|
||
module Fabricio | ||
module Authorization | ||
# Stores default params as organization, app, etc. | ||
class FileParamStorage < AbstractParamStorage | ||
|
||
# Returns all stored variable | ||
# | ||
# @return [Hash] | ||
def obtain | ||
return nil unless File.exist?(PARAM_FILE_PATH) | ||
return YAML.load_file(PARAM_FILE_PATH) || {} | ||
end | ||
|
||
# Save variable | ||
# | ||
# @param hash [Hash] | ||
def store(hash) | ||
save_to_file(hash) | ||
end | ||
|
||
# Resets current state and deletes all saved params | ||
def reset_all | ||
FileUtils.remove_file(PARAM_FILE_PATH) | ||
end | ||
|
||
def organization_id | ||
obtain['organization_id'] | ||
end | ||
|
||
def app_id | ||
obtain['app_id'] | ||
end | ||
|
||
def store_organization_id(organization_id) | ||
hash = obtain || {} | ||
hash['organization_id'] = organization_id | ||
save_to_file(hash) | ||
end | ||
|
||
def store_app_id(app_id) | ||
hash = obtain || {} | ||
hash['app_id'] = app_id | ||
save_to_file(hash) | ||
end | ||
|
||
private | ||
|
||
def save_to_file(hash) | ||
FileUtils.mkdir_p(FABRICIO_DIRECTORY_PATH) | ||
File.open(SESSION_FILE_PATH,'w') do |f| | ||
f.write hash.to_yaml | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
require 'fabricio/authorization/abstract_session_storage' | ||
require 'fileutils' | ||
require 'yaml' | ||
|
||
# Constants | ||
FABRICIO_DIRECTORY_PATH = "#{Dir.home}/.fabricio" | ||
SESSION_FILE_PATH = "#{CREDENTIAL_DIRECTORY_PATH}/.session" | ||
|
||
module Fabricio | ||
module Authorization | ||
# The only one built-in session storage. Stores current session in file. | ||
class FileSessionStorage < AbstractSessionStorage | ||
|
||
# Returns session stored in a variable | ||
# | ||
# @return [Fabricio::Authorization::Session] | ||
def obtain_session | ||
return nil unless File.exist?(SESSION_FILE_PATH) | ||
session_hash = YAML.load_file(SESSION_FILE_PATH) | ||
session = Session(session_hash) | ||
return nil unless session.access_token | ||
return nil unless session.refresh_token | ||
session | ||
end | ||
|
||
# Stores session in a variable | ||
# | ||
# @param session [Fabricio::Authorization::MemorySessionStorage] | ||
def store_session(session) | ||
FileUtils.mkdir_p(FABRICIO_DIRECTORY_PATH) | ||
File.open(SESSION_FILE_PATH,'w') do |f| | ||
f.write session.hash.to_yaml | ||
end | ||
end | ||
|
||
# Resets current state and deletes saved session | ||
def reset | ||
FileUtils.remove_file(SESSION_FILE_PATH) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
require 'fabricio/authorization/abstract_param_storage' | ||
|
||
module Fabricio | ||
module Authorization | ||
# Stores default params as organization, app, etc. | ||
class MemoryParamStorage < AbstractParamStorage | ||
|
||
# Initializes a new ParamStorage object | ||
# | ||
# @return [Fabricio::Authorization::ParamStorage] | ||
def initialize | ||
@params = nil | ||
end | ||
|
||
# Returns all stored variable | ||
# | ||
# @return [Hash] | ||
def obtain | ||
@params | ||
end | ||
|
||
# Save variable | ||
# | ||
# @param hash [Hash] | ||
def store(hash) | ||
@params = hash | ||
end | ||
|
||
# Resets current state and deletes all saved params | ||
def reset_all | ||
@params = nil | ||
end | ||
|
||
def organization_id | ||
obtain['organization_id'] | ||
end | ||
|
||
def app_id | ||
obtain['app_id'] | ||
end | ||
|
||
def store_organization_id(organization_id) | ||
hash = obtain || {} | ||
hash['organization_id'] = organization_id | ||
@params = hash | ||
end | ||
|
||
def store_app_id(app_id) | ||
hash = obtain || {} | ||
hash['app_id'] = app_id | ||
@params = hash | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,4 +32,4 @@ def reset | |
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.