forked from piotrmurach/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthorization.rb
More file actions
73 lines (63 loc) · 2.1 KB
/
authorization.rb
File metadata and controls
73 lines (63 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
71
72
73
# encoding: utf-8
module Github
module Authorization
# Setup OAuth2 instance
def client
@client ||= ::OAuth2::Client.new(client_id, client_secret,
{
:site => current_options.fetch(:site) { Github.site },
:authorize_url => 'login/oauth/authorize',
:token_url => 'login/oauth/access_token',
:ssl => { :verify => false }
}
)
end
# Strategy token
def auth_code
_verify_client
client.auth_code
end
# Sends authorization request to GitHub.
# = Parameters
# * <tt>:redirect_uri</tt> - Optional string.
# * <tt>:scope</tt> - Optional string. Comma separated list of scopes.
# Available scopes:
# * (no scope) - public read-only access (includes public user profile info, public repo info, and gists).
# * <tt>user</tt> - DB read/write access to profile info only.
# * <tt>public_repo</tt> - DB read/write access, and Git read access to public repos.
# * <tt>repo</tt> - DB read/write access, and Git read access to public and private repos.
# * <tt>gist</tt> - write access to gists.
#
def authorize_url(params = {})
_verify_client
client.auth_code.authorize_url(params)
end
# Makes request to token endpoint and retrieves access token value
def get_token(authorization_code, params = {})
_verify_client
client.auth_code.get_token(authorization_code, params)
end
# Check whether authentication credentials are present
def authenticated?
basic_authed? || oauth_token?
end
# Check whether basic authentication credentials are present
def basic_authed?
basic_auth? || (login? && password?)
end
# Select authentication parameters
#
# @api public
def authentication
if basic_authed?
{ login: login, password: password }
else
{}
end
end
private
def _verify_client # :nodoc:
raise ArgumentError, 'Need to provide client_id and client_secret' unless client_id? && client_secret?
end
end # Authorization
end # Github