Commit 73b390946abf7eafe8999641c281d4f259e1bce3
Committed by
Larissa Reis
1 parent
2a4c9e64
Exists in
oauth_external_login
and in
1 other branch
Add test structure
Signed-off-by: Alessandro Caetano <alessandro.caetanob@gmail.com>
Showing
2 changed files
with
42 additions
and
31 deletions
Show diff stats
lib/noosfero/api/federation/webfinger.rb
| ... | ... | @@ -12,9 +12,13 @@ module Noosfero |
| 12 | 12 | end |
| 13 | 13 | end |
| 14 | 14 | |
| 15 | -def extract_person_identifier | |
| 16 | - person = params[:resource].split("@")[0].split(":")[1] | |
| 17 | - person | |
| 15 | +def generate_jrd | |
| 16 | + result = {} | |
| 17 | + if valid_domain? && request_acct? | |
| 18 | + result = acct_hash | |
| 19 | + elsif valid_domain? && valid_uri?(params[:resource]) | |
| 20 | + result = uri_hash | |
| 21 | + end | |
| 18 | 22 | end |
| 19 | 23 | |
| 20 | 24 | def valid_domain? |
| ... | ... | @@ -28,32 +32,6 @@ def valid_domain? |
| 28 | 32 | end |
| 29 | 33 | end |
| 30 | 34 | |
| 31 | -def valid_uri?(url) | |
| 32 | - uri = URI.parse(url) | |
| 33 | - uri.kind_of?(URI::HTTP) | |
| 34 | - rescue URI::BadURIError | |
| 35 | - false | |
| 36 | - rescue URI::InvalidURIError | |
| 37 | - false | |
| 38 | -end | |
| 39 | - | |
| 40 | -def entity_exists?(uri) | |
| 41 | - possible_entity = uri.split('/') | |
| 42 | - possible_entity.map! {|entity| "#{entity}s"} | |
| 43 | - ( ActiveRecord::Base.connection.tables & possible_entity ) | |
| 44 | - rescue ActiveRecord::RecordNotFound | |
| 45 | - false | |
| 46 | -end | |
| 47 | - | |
| 48 | -def generate_jrd | |
| 49 | - result = {} | |
| 50 | - if valid_domain? && request_acct? | |
| 51 | - result = acct_hash | |
| 52 | - elsif valid_uri?(params[:resource]) | |
| 53 | - result = uri_hash | |
| 54 | - end | |
| 55 | -end | |
| 56 | - | |
| 57 | 35 | def request_acct? |
| 58 | 36 | params[:resource].include? "acct:" |
| 59 | 37 | end |
| ... | ... | @@ -65,13 +43,34 @@ def acct_hash |
| 65 | 43 | acct |
| 66 | 44 | end |
| 67 | 45 | |
| 46 | +def extract_person_identifier | |
| 47 | + person = params[:resource].split("@")[0].split(":")[1] | |
| 48 | +end | |
| 49 | + | |
| 50 | +def valid_uri?(url) | |
| 51 | + uri = URI.parse(url) | |
| 52 | + uri.kind_of?(URI::HTTP) | |
| 53 | + rescue URI::BadURIError | |
| 54 | + "URI is valid, bad usage is not" | |
| 55 | + rescue URI::InvalidURIError | |
| 56 | + "invalid URI" | |
| 57 | +end | |
| 58 | + | |
| 68 | 59 | def uri_hash |
| 69 | 60 | uri = {} |
| 70 | 61 | uri[:subject] = params[:resource] |
| 71 | 62 | entity = entity_exists?(params[:resource]) |
| 72 | - noosfero_entity = ActiveSupport::Inflector.classify(entity) | |
| 73 | 63 | id = params[:resource].split('/').last.to_i |
| 74 | - uri[:properties] = entity.first.classify.constantize.find(id) | |
| 64 | + uri[:properties] = entity.classify.constantize.find(id) | |
| 75 | 65 | puts uri.inspect |
| 76 | 66 | uri |
| 77 | 67 | end |
| 68 | + | |
| 69 | +def entity_exists?(uri) | |
| 70 | + possible_entity = uri.split('/') | |
| 71 | + possible_entity.map! {|entity| "#{entity}s"} | |
| 72 | + ( ActiveRecord::Base.connection.tables & possible_entity ).first | |
| 73 | + rescue ActiveRecord::RecordNotFound | |
| 74 | + false | |
| 75 | +end | |
| 76 | + | ... | ... |