class Zzlink::Auth

Helpers of visiting SSO service.

Public Instance Methods

check_in_with_ticket(ticket) click to toggle source

Check in with a ticket. @param ticket [String] the SSO ticket. @return [Session] a session object if there is a valid session. @raise if check-in failed.

# File lib/zzlink/auth.rb, line 34
def check_in_with_ticket(ticket)
  check_in(ticket: ticket)
end
check_in_with_user_token(token) click to toggle source

Check in with a token. @param token [String] the user token. @return [Session] a session object if there is a valid session. @raise if check-in failed.

# File lib/zzlink/auth.rb, line 42
def check_in_with_user_token(token)
  check_in(user_token: token)
end
url_of_login(redirect_url, client_id = 'web', ttl = 600) click to toggle source

Get login URL for the local redirect URL. @param redirect_url [String] @param client_id [String] client identification. Default is a web APP. @param ttl [Integer] TTL (in seconds) of the login procedure. @return [String] login URL of SSO.

# File lib/zzlink/auth.rb, line 15
def url_of_login(redirect_url, client_id = 'web', ttl = 600)
  query = URI.encode_www_form(redirect_url: redirect_url, zt: zzlink_ticket(ttl), zc: client_id)
  sso_url + '/login?' + query
end
url_of_login_as_github(redirect_url, client_id = 'web', ttl = 600) click to toggle source

Get login URL of Github for the local redirect URL. @param redirect_url [String] @param client_id [String] client identification. Default is a web APP. @param ttl [Integer] TTL (in seconds) of the login procedure. @return [String] login URL of SSO.

# File lib/zzlink/auth.rb, line 25
def url_of_login_as_github(redirect_url, client_id = 'web', ttl = 600)
  query = URI.encode_www_form(redirect_url: redirect_url, zt: zzlink_ticket(ttl), zc: client_id)
  sso_url + '/oauth/github?' + query
end
url_of_logout(redirect_url, ttl = 60) click to toggle source

Get logout URL for the local redirect URL. @param redirect_url [String] @param ttl [Integer] TTL (in seconds) of the login procedure. @return [String] logout URL of SSO.

# File lib/zzlink/auth.rb, line 50
def url_of_logout(redirect_url, ttl = 60)
  query = URI.encode_www_form(redirect_url: redirect_url, zt: zzlink_ticket(ttl))
  sso_url + '/logout?' + query
end

Private Instance Methods

check_in(param) click to toggle source

Check session status with ticket or user token. @param param [Hash] {ticket: 'xxxx'} or {user_token: 'xxxx'}. @return [Session] a session object in case of that a session is alive. @raise if check-in failed.

# File lib/zzlink/auth.rb, line 69
def check_in(param)
  query = URI.encode_www_form(param)
  uri = URI(sso_url + '/checkin?' + query)
  resp = Net::HTTP.start(uri.host, uri.port, nil, nil, nil, nil, uss_ssl: uri.scheme == 'https') do |http|
    req = Net::HTTP::Put.new(uri.request_uri)
    set_request_headers(req)
    req['Content-Type'] = 'application/json'
    req.body = param.to_json
    http.request(req)
  end
  value = check_and_return(resp)
  Session.new(value)
end
sso_url() click to toggle source
# File lib/zzlink/auth.rb, line 57
def sso_url
  Zzlink.env.get_env('sso_url')
end