Commit fedb0d62112cd6962ea2a738ff3ce544408e7166
Committed by
Alessandro Beltrão
1 parent
110c9f56
Exists in
federation_webfinger
and in
1 other branch
Federation webfinger
Showing
2 changed files
with
54 additions
and
8 deletions
Show diff stats
lib/noosfero/api/api.rb
| @@ -5,14 +5,12 @@ Dir["#{Rails.root}/lib/noosfero/api/*.rb"].each {|file| require file unless file | @@ -5,14 +5,12 @@ Dir["#{Rails.root}/lib/noosfero/api/*.rb"].each {|file| require file unless file | ||
| 5 | module Noosfero | 5 | module Noosfero |
| 6 | module API | 6 | module API |
| 7 | 7 | ||
| 8 | - class Federation < Grape::API | 8 | + class NoosferoFederation < Grape::API |
| 9 | + before { detect_stuff_by_domain } | ||
| 9 | format :json | 10 | format :json |
| 10 | - content_type :txt, "text/plain" | ||
| 11 | - | ||
| 12 | - #REMOVE ME - testing routes only | ||
| 13 | - get "/me" do | ||
| 14 | - present_partial current_person, :with => Entities::Person, :current_person => current_person | ||
| 15 | - end | 11 | + content_type :json, "application/jrd+json" |
| 12 | + prefix ".well-known" | ||
| 13 | + mount Federation::Webfinger | ||
| 16 | end | 14 | end |
| 17 | 15 | ||
| 18 | class BaseAPI < Grape::API | 16 | class BaseAPI < Grape::API |
| @@ -70,7 +68,7 @@ module Noosfero | @@ -70,7 +68,7 @@ module Noosfero | ||
| 70 | end | 68 | end |
| 71 | 69 | ||
| 72 | mount Noosfero::API::BaseAPI | 70 | mount Noosfero::API::BaseAPI |
| 73 | - mount Noosfero::API::Federation | 71 | + mount Noosfero::API::NoosferoFederation |
| 74 | 72 | ||
| 75 | # hook point which allow plugins to add Grape::API extensions to API::API | 73 | # hook point which allow plugins to add Grape::API extensions to API::API |
| 76 | #finds for plugins which has api mount points classes defined (the class should extends Grape::API) | 74 | #finds for plugins which has api mount points classes defined (the class should extends Grape::API) |
| @@ -0,0 +1,48 @@ | @@ -0,0 +1,48 @@ | ||
| 1 | +module Noosfero | ||
| 2 | + module API | ||
| 3 | + module Federation | ||
| 4 | + class Webfinger < Grape::API | ||
| 5 | + get "webfinger" do | ||
| 6 | + result = generate_jrd | ||
| 7 | + present result, :with => Grape::Presenters::Presenter | ||
| 8 | + end | ||
| 9 | + end | ||
| 10 | + end | ||
| 11 | + end | ||
| 12 | +end | ||
| 13 | + | ||
| 14 | +def extract_person_identifier | ||
| 15 | + person = params[:resource].split("@")[0].split(":")[1] | ||
| 16 | + person | ||
| 17 | +end | ||
| 18 | + | ||
| 19 | +def valid_domain? | ||
| 20 | + #validate domain if resource have acct | ||
| 21 | + if request_acct? | ||
| 22 | + domain = params[:resource].split("@")[1] | ||
| 23 | + environment.domains.map(&:name).include? domain | ||
| 24 | + else | ||
| 25 | + #please validate domain with http | ||
| 26 | + false | ||
| 27 | + end | ||
| 28 | +end | ||
| 29 | + | ||
| 30 | +def generate_jrd | ||
| 31 | + if valid_domain? && request_acct? | ||
| 32 | + result = {} | ||
| 33 | + result = acct_hash | ||
| 34 | + else | ||
| 35 | + "This Domain this not exist in this envinronment" | ||
| 36 | + end | ||
| 37 | +end | ||
| 38 | + | ||
| 39 | +def request_acct? | ||
| 40 | + params[:resource].include? "acct:" | ||
| 41 | +end | ||
| 42 | + | ||
| 43 | +def acct_hash | ||
| 44 | + acct = {} | ||
| 45 | + acct[:subject] = params[:resource] | ||
| 46 | + acct[:properties] = Person.find_by_identifier(extract_person_identifier) | ||
| 47 | + acct | ||
| 48 | +end |