Class: Rnote::Auth

Inherits:
Object
  • Object
show all
Defined in:
lib/rnote/auth.rb

Instance Method Summary collapse

Constructor Details

#initialize(persister = Persister.new) ⇒ Auth

Returns a new instance of Auth.



12
13
14
# File 'lib/rnote/auth.rb', line 12

def initialize(persister=Persister.new)
  @persister = persister
end

Instance Method Details

#clientObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rnote/auth.rb', line 72

def client
  # not the same as the client used to get the token.
  # this one is fully authorized and can make actual api calls.

  if not is_logged_in
    raise "not logged in"
  end
  
  token = @persister.get_user_token || @persister.get_developer_token
  
  @client ||= EvernoteOAuth::Client.new(token: token, sandbox: @persister.get_sandbox)

  @client
end

#is_logged_inObject



118
119
120
# File 'lib/rnote/auth.rb', line 118

def is_logged_in
  @persister.get_user_token or @persister.get_developer_token
end

#login_with_developer_token(developer_token, sandbox) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rnote/auth.rb', line 16

def (developer_token,sandbox)
  if is_logged_in
    if @persister.get_developer_token == developer_token
      return
    else
      logout
    end
  end
  @persister.persist_developer_token(developer_token)
  @persister.persist_sandbox(sandbox)
end

#login_with_password(username, password, sandbox) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rnote/auth.rb', line 28

def (username,password,sandbox)
  
  if is_logged_in
    if who == username
      # already logged in (we don't check against service though)
      # if a re-login is truely required, the user can just logout first.
      return
    else
      logout
    end
  end

  ## Consumer Key and Secret provided in published gem.
  #
  # we'll use these if the user doesn't provide their own
  # we do this check here, instead of in Persister,
  # so we can verify this is only used in production, not sandbox.
  #

  consumer_key = @persister.get_consumer_key || ( ! sandbox && PRODUCTION_CONSUMER_KEY )
  raise 'no consumer key to use, please provide one.' unless consumer_key
  consumer_secret = @persister.get_consumer_secret || ( ! sandbox && PRODUCTION_CONSUMER_SECRET )
  raise 'no consumer secret to use, please provide one.' unless consumer_secret
  
  ## Get a user key using these crednetials
  
  # this client isn't authorized, and can only request authorization. no api calls.
  auth_client = EvernoteOAuth::Client.new(
      consumer_key: consumer_key,
      consumer_secret: consumer_secret,
      sandbox: sandbox
  )

  request_token = auth_client.request_token(:oauth_callback => DUMMY_CALLBACK_URL)
  oauth_verifier = (request_token.authorize_url, username, password)
  access_token = request_token.get_access_token(oauth_verifier: oauth_verifier)
  user_token = access_token.token

  @persister.persist_username(username)
  @persister.persist_user_token(user_token)
  @persister.persist_sandbox(sandbox)

end

#logoutObject



131
132
133
134
135
136
137
138
# File 'lib/rnote/auth.rb', line 131

def logout
  # unfortunately, no way to revoke a token via API
  # TODO perhaps I can redo the oauth, and choose revoke instead of re-accept
  @persister.forget_user_token
  @persister.forget_username
  @persister.forget_developer_token
  @persister.forget_sandbox
end

#mechanize_login(url, username, password) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rnote/auth.rb', line 91

def (url, username, password)

  agent = Mechanize.new
   = agent.get(url)
   = .form('login_form')
  raise unless 
  .username = username
  .password = password
  accept_page = agent.submit(,.buttons.first)

  if accept_page.form('login_form')
    # sent us back to the login page
    raise "bad username/password"
  elsif not accept_page.form('oauth_authorize_form')
    raise "failed to login"
  end

  accept_form = accept_page.form('oauth_authorize_form')
  # we don't need to go so far as to retrieve the callback url.
  agent.redirect_ok = false
  callback_redirect = agent.submit(accept_form, accept_form.buttons.first)
  response_url = callback_redirect.response['location']
  oauth_verifier = CGI.parse(URI.parse(response_url).query)['oauth_verifier'][0]

  oauth_verifier
end

#note_storeObject



87
88
89
# File 'lib/rnote/auth.rb', line 87

def note_store
  client.note_store
end

#whoObject



122
123
124
125
126
127
128
# File 'lib/rnote/auth.rb', line 122

def who
  if is_logged_in
    @persister.get_username or @persister.get_developer_token
  else
    nil
  end
end