Module: SlackOauth::Driver::Helper

Defined in:
lib/helper.rb

Instance Method Summary collapse

Instance Method Details

#authorize(code) ⇒ Object



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

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[:slack_authorized] = res['ok'] && settings.slack_allowed_teams.include?(res['team_name'])

  if session[:slack_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[:slack_authorized]
end

#authorized?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/helper.rb', line 39

def authorized?
  session[:slack_authorized]
end

#get_authentication_urlObject



71
72
73
# File 'lib/helper.rb', line 71

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

#get_paramsObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/helper.rb', line 47

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

  @slack_state_generator ||= has_settings(:slack_state_generator) ?  settings.slack_state_generator : ->{
    SecureRandom.hex(32)
  }

  session[:slack_state] = @slack_state_generator.call
  params << "state=#{session[:slack_state]}"

  "?#{params.join('&')}"
end

#validate_state(state) ⇒ Object



43
44
45
# File 'lib/helper.rb', line 43

def validate_state(state)
  session[:slack_state] == state
end