Class: ProximityBeacon::OAuth

Inherits:
Object
  • Object
show all
Defined in:
lib/proximity_beacon/oauth.rb

Constant Summary collapse

SCOPES =
[
  "https://www.googleapis.com/auth/userlocation.beacon.registry",
  "https://www.googleapis.com/auth/cloud-platform",
]
REDIRECT_URI =
"urn:ietf:wg:oauth:2.0:oob"
TOKEN_URI =
URI("https://www.googleapis.com/oauth2/v4/token")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(redirect_uri = REDIRECT_URI, client_id = , secret = ) ⇒ OAuth

Returns a new instance of OAuth.



18
19
20
21
22
23
24
25
# File 'lib/proximity_beacon/oauth.rb', line 18

def initialize(redirect_uri = REDIRECT_URI, client_id = ENV["GOOGLE_CLIENT_ID"], secret = ENV["GOOGLE_CLIENT_SECRET"])
  if client_id.nil? || secret.nil?
    raise ArgumentError, "No Google Client ID or Secret was set. These can set in the environment variables \"GOOGLE_CLIENT_ID\" and \"GOOGLE_CLIENT_SECRET\" respectively. Credentials must be created for your project at \"https://console.developers.google.com/apis/credentials\"."
  end
  self.client_id = client_id
  self.secret = secret
  self.redirect_uri = redirect_uri
end

Instance Attribute Details

#client_idObject

Returns the value of attribute client_id.



16
17
18
# File 'lib/proximity_beacon/oauth.rb', line 16

def client_id
  @client_id
end

#redirect_uriObject

Returns the value of attribute redirect_uri.



16
17
18
# File 'lib/proximity_beacon/oauth.rb', line 16

def redirect_uri
  @redirect_uri
end

#secretObject

Returns the value of attribute secret.



16
17
18
# File 'lib/proximity_beacon/oauth.rb', line 16

def secret
  @secret
end

Instance Method Details

#get_codeObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/proximity_beacon/oauth.rb', line 27

def get_code
  puts("Performing OAuth with Google...")
  if RUBY_PLATFORM =~ /darwin/
    system("open \"#{url}\"")
  else
    puts("Open this URL in your browser: \"#{url}\"\n\n")
  end
  printf "Copy and paste code from web browser here: "
  _code = STDIN.gets.chomp
end

#get_credentials(code = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/proximity_beacon/oauth.rb', line 38

def get_credentials(code = nil)
  response = Client::Request.post(TOKEN_URI) {|request|
    request.body = hash_to_params(
      code: code || get_code,
      client_id: client_id,
      client_secret: secret,
      redirect_uri: redirect_uri,
      grant_type: "authorization_code",
    )
  }
  Credentials.new(JSON.parse(response.body))
end

#refresh_credentials(credentials) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/proximity_beacon/oauth.rb', line 51

def refresh_credentials(credentials)
  response = Client::Request.post(TOKEN_URI) {|request|
    request.body = hash_to_params(
      refresh_token: credentials.refresh_token,
      client_id: client_id,
      client_secret: secret,
      grant_type: "refresh_token",
    )
  }
  json = JSON.parse(response.body)
  new_credentials = Credentials.new(json)
  new_credentials.refresh_token = credentials.refresh_token
  new_credentials
end

#urlObject



66
67
68
69
70
71
72
73
74
# File 'lib/proximity_beacon/oauth.rb', line 66

def url
  params = hash_to_params(
    scope: SCOPES.join("%20"),
    redirect_uri: redirect_uri,
    response_type: "code",
    client_id: client_id,
  )
  "https://accounts.google.com/o/oauth2/v2/auth?#{params}"
end