Class: Modgen::Session::Oauth2

Inherits:
Object
  • Object
show all
Defined in:
lib/modgen/session/oauth2.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOauth2

Returns a new instance of Oauth2.



76
77
78
79
80
# File 'lib/modgen/session/oauth2.rb', line 76

def initialize
  @client = OAuth2::Client.new(client_id, client_secret, site: Modgen::SITE_URL)

  @authorize_url = client.auth_code.authorize_url(redirect_uri: redirect_uri)
end

Instance Attribute Details

#authorize_urlObject (readonly)

Returns the value of attribute authorize_url.



74
75
76
# File 'lib/modgen/session/oauth2.rb', line 74

def authorize_url
  @authorize_url
end

Class Method Details

.authorize(auth_code) ⇒ Object



26
27
28
# File 'lib/modgen/session/oauth2.rb', line 26

def self.authorize(auth_code)
  Modgen::Session.get.authorize(auth_code)
end

.configObject



7
8
9
# File 'lib/modgen/session/oauth2.rb', line 7

def self.config
  Modgen.config
end

.get_authorize_codeObject

Automaticaly open autorization url a waiting for callback. Launchy gem is required

Steps: 1) create server 2) launch browser and redirect to google api 3) confirm and modgen redirect to localhost 4) server get code and start session 5) close server - you are login



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/modgen/session/oauth2.rb', line 39

def self.get_authorize_code
  require "socket"  # tcp server

  uri = URI(Modgen::OAUTH2_REDIRECT_URI)
  
  webserver = TCPServer.new(uri.host, 0) # start webserver
                                         # port=0 - automatically choose free port

  uri.port = webserver.addr[1] # get choosen port

  Launchy.open(Modgen::Session.get.authorize_url)

  session = webserver.accept

  # parse header for query.
  request = session.gets.gsub(/GET\ \//, '').gsub(/\ HTTP.*/, '')
  request = Hash[URI.decode_www_form(URI(request).query)]

  # failure login
  to_return = false
  message   = "You have not been logged. Please try again."

  if Modgen::Session.get.authorize(request['code'])
    message   = "You have been successfully logged. Now you can close the browser."
    to_return = true
  end

  session.write(message)
  session.close

  return to_return
end

.startObject

Start session



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/modgen/session/oauth2.rb', line 14

def self.start
  client = Modgen::Session::Oauth2.new

  Modgen::Session.store(client)

  if config.oauth2.redirect_uri == nil
    get_authorize_code
  else
    client.authorize_url
  end
end

Instance Method Details

#authorize(auth_code) ⇒ Object



82
83
84
85
86
# File 'lib/modgen/session/oauth2.rb', line 82

def authorize(auth_code)
  @auth_code = auth_code

  @token = @client.auth_code.get_token(auth_code)
end

#execute(request) ⇒ Object



88
89
90
# File 'lib/modgen/session/oauth2.rb', line 88

def execute(request)
  
end