Class: DiasporaClient::App

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/diaspora-client/app.rb

Instance Method Summary (collapse)

Instance Method Details

- (void) /

This method returns an undefined value.

Destroy the current user's access token and redirect.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/diaspora-client/app.rb', line 53

get '/' do

  # ensure faraday is configured
  DiasporaClient.setup_faraday

  begin
    redirect client.web_server.authorize_url(
      :redirect_uri => redirect_uri,
      :scope => 'profile,AS_photo:post'
    )
  rescue Exception => e
    redirect_url = back.to_s
    if defined?(Rails)
      flash_class = ActionDispatch::Flash
      flash = request.env["action_dispatch.request.flash_hash"] ||= flash_class::FlashHash.new
      flash.alert = e.message
    else
      redirect_url << "?diaspora-client-error=#{URI.escape(e.message)}"
    end
    redirect redirect_url
  end
end

- (void) /callback

This method returns an undefined value.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/diaspora-client/app.rb', line 77

get '/callback' do
  if !params["error"]
    access_token = client.web_server.get_access_token(params[:code], :redirect_uri => redirect_uri)
    user = JSON.parse(access_token.get('/api/v0/me'))

    current_user.create_access_token(
      :uid => user["uid"],
      :resource_server_id => pod.id,
      :access_token => access_token.token,
      :refresh_token => access_token.refresh_token,
      :expires_at => access_token.expires_at
    )
  elsif params["error"] == "invalid_client"
    ResourceServer.register(diaspora_handle.split('@')[1])
    redirect "/?diaspora_handle=#{diaspora_handle}"
  end

  redirect after_oauth_redirect_path
end

- (String) after_oauth_redirect_path

The path to send the user after the OAuth2 dance is complete.

Returns:

  • (String)

    The path to send the user after the OAuth2 dance is complete.



34
35
36
# File 'lib/diaspora-client/app.rb', line 34

def after_oauth_redirect_path
  '/users/edit'
end

- (OAuth2::Client) client

The connecting Diaspora installation's Client object.

Returns:

  • (OAuth2::Client)

    The connecting Diaspora installation's Client object.

See Also:



6
7
8
# File 'lib/diaspora-client/app.rb', line 6

def client
  pod.client
end

- (User) current_user

The current user stored in warden.

Returns:

  • (User)

    The current user stored in warden.



48
49
50
# File 'lib/diaspora-client/app.rb', line 48

def current_user
  request.env["warden"].user
end

- (String) diaspora_handle

Retreive the user's Diaspora handle from the params hash.

Returns:

  • (String)


24
25
26
# File 'lib/diaspora-client/app.rb', line 24

def diaspora_handle
  @diaspora_handle ||= params['diaspora_handle'].strip
end

- (ResourceServer) pod

Note:

The Diaspora server is parsed from the domain in the given diaspora handle.

Find a pre-existing Diaspora server, or register with a new one.

Returns:



14
15
16
17
18
19
# File 'lib/diaspora-client/app.rb', line 14

def pod
  @pod ||= lambda{
    host = diaspora_handle.split('@')[1]
    ResourceServer.where(:host => host).first || ResourceServer.register(host)
  }.call
end

- (String) redirect_path

The path to hit after retreiving an access token from a Diaspora server.

Returns:

  • (String)

    The path to hit after retreiving an access token from a Diaspora server.



29
30
31
# File 'lib/diaspora-client/app.rb', line 29

def redirect_path
  '/auth/diaspora/callback'
end

- (String) redirect_uri

The URL to hit after retreiving an access token from a Diaspora server.

Returns:

  • (String)

    The URL to hit after retreiving an access token from a Diaspora server.

See Also:



40
41
42
43
44
45
# File 'lib/diaspora-client/app.rb', line 40

def redirect_uri
  uri = Addressable::URI.parse(request.url)
  uri.path = redirect_path
  uri.query_values = {:diaspora_handle => diaspora_handle}
  uri.to_s
end