class Naver::Oauth

Public Class Methods

new(redirect_uri: Naver.redirect_uri) click to toggle source

Create a OAuth object. @param redirect_uri [String] OAuth redirect_uri

# File lib/naver/oauth.rb, line 5
def initialize(redirect_uri: Naver.redirect_uri)
  @client_id     = Naver.client_id
  @client_secret = Naver.client_secret
  @oauth_base_uri = Configuration::DEFAULT_OAUTH_BASE_URI
  @redirect_uri = redirect_uri

  headers = { user_agent: Configuration::DEFAULT_USER_AGENT }
  @oauth = OAuth2::Client.new(@client_id, @client_secret, site: @oauth_base_uri, authorize_url: "/oauth2.0/authorize", token_url: "/oauth2.0/token", headers: headers) do |http|
    http.request :multipart
    http.request :url_encoded
    http.response :logger if Naver.debug
    http.adapter :net_http
  end
end

Public Instance Methods

authorization_url(requested_scopes: ["public"]) click to toggle source

Get OAuth URL for user authentication and authorization. @param requested_scopes [Array] An array of permission scopes being requested. @return [String] The authorization URL.

# File lib/naver/oauth.rb, line 23
def authorization_url(requested_scopes: ["public"])
  @oauth.auth_code.authorize_url(
    redirect_uri: @redirect_uri,
    scope: requested_scopes.join("")
  )
end
authorize!(auth_code) click to toggle source

Generate an access token given an auth code received from NAVER. This is used internally to authenticate and authorize future user actions. @param auth_code [String] The OAuth authentication code from NAVER.

# File lib/naver/oauth.rb, line 33
def authorize!(auth_code)
  @oauth_token = @oauth.auth_code.get_token(auth_code, redirect_uri: @redirect_uri)
  # TODO check if it succeeded
end
create_and_assign_token(token_extract) click to toggle source

Create and assign new access token from extracted token. To be used with extract_token to reauthorize app without api call. @param token_extract [Hash] OAuth token hash from extract_token. @return [OAuth2::AccessToken, nil] New access token object.

# File lib/naver/oauth.rb, line 50
def create_and_assign_token(token_extract)
  unless token_extract.nil?
    @oauth_token = OAuth2::AccessToken.from_hash(@oauth, token_extract)
  end
end
extract_token() click to toggle source

Extract hash with OAuth token attributes. Extracted token attributes can be used with create_and_assign_token to prevent the need for reauthorization. @return [Hash, nil] @oauth_token converted to a hash.

# File lib/naver/oauth.rb, line 42
def extract_token
  @oauth_token.to_hash if @oauth_token
end

Private Instance Methods

refresh_token!() click to toggle source
# File lib/naver/oauth.rb, line 58
def refresh_token!
  return unless @oauth_token.expired?
  @oauth_token = @oauth_token.refresh_token
end