Module: SlackOauth::Driver::Helper

Defined in:
lib/helper.rb

Instance Method Summary collapse

Instance Method Details

#authorize(code) ⇒ Object



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
# File 'lib/helper.rb', line 9

def authorize(code)
  uri = URI.parse('https://slack.com/api/oauth.access')
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  req = Net::HTTP::Post.new(uri.path)

  params = {client_id: settings.slack_client_id,
            client_secret: settings.slack_secret_key,
            code: code}

  if has_settings(:slack_redirect_uri)
    params[:redirect_uri] = settings.slack_redirect_uri
  end
    
  req.set_form_data(params)
  res = JSON.parse(http.request(req).body)
  session[:authorized] = res['ok'] && settings.slack_allowed_teams.include?(res['team_name'])
  if session[:authorized]
    session[:slack_team] = res['team_name']
    session[:slack_access_token] = res['access_token']
    session[:slack_user_id] = res['user_id']
    session[:slack_team_id] = res['team_id']
  end
  session[:authorized]
end

#authorized?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/helper.rb', line 36

def authorized?
  session[:authorized]
end

#get_authentication_urlObject



56
57
58
# File 'lib/helper.rb', line 56

def get_authentication_url
  "https://slack.com/oauth/authorize#{get_params}"
end

#get_paramsObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/helper.rb', line 40

def get_params
  params = []

  params << "client_id=#{settings.slack_client_id}"
  params << "scope=#{settings.slack_scope}"

  if has_settings(:slack_team)
    params << "team=#{settings.slack_team}"
  end

  if has_settings(:slack_redirect_uri)
    params << "redirect_uri=#{settings.slack_redirect_uri}"
  end
  "?#{params.join('&')}"
end