Class: Naver::Oauth

Inherits:
Object
  • Object
show all
Defined in:
lib/naver/oauth.rb

Instance Method Summary collapse

Constructor Details

#initialize(redirect_uri: Naver.redirect_uri) ⇒ Oauth

Create a OAuth object.

Parameters:

  • redirect_uri (String) (defaults to: Naver.redirect_uri)

    OAuth redirect_uri



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 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

Instance Method Details

#authorization_url(requested_scopes: ["public"]) ⇒ String

Get OAuth URL for user authentication and authorization.

Parameters:

  • requested_scopes (Array) (defaults to: ["public"])

    An array of permission scopes being requested.

Returns:

  • (String)

    The authorization URL.



23
24
25
26
27
28
# 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) ⇒ Object

Generate an access token given an auth code received from NAVER. This is used internally to authenticate and authorize future user actions.

Parameters:

  • auth_code (String)

    The OAuth authentication code from NAVER.



33
34
35
36
# 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) ⇒ OAuth2::AccessToken?

Create and assign new access token from extracted token. To be used with extract_token to reauthorize app without api call.

Parameters:

  • token_extract (Hash)

    OAuth token hash from #extract_token.

Returns:

  • (OAuth2::AccessToken, nil)

    New access token object.



50
51
52
53
54
# 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_tokenHash?

Extract hash with OAuth token attributes. Extracted token attributes can be used with create_and_assign_token to prevent the need for reauthorization.

Returns:

  • (Hash, nil)

    @oauth_token converted to a hash.



42
43
44
# File 'lib/naver/oauth.rb', line 42

def extract_token
  @oauth_token.to_hash if @oauth_token
end