Class: Naver::Oauth

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

Constant Summary collapse

DEFAULT_OAUTH_BASE_URI =

Base URI for NAVER OAuth. DEFAULT_OAUTH_BASE_URI = “nid.naver.com/oauth2.0

"https://nid.naver.com"

Instance Method Summary collapse

Constructor Details

#initialize(oauth_base_uri: DEFAULT_OAUTH_BASE_URI, redirect_uri: Naver.redirect_uri) ⇒ Oauth

Create a Oauth object.


9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/naver/oauth.rb', line 9

def initialize(oauth_base_uri: DEFAULT_OAUTH_BASE_URI, redirect_uri: Naver.redirect_uri)
  @client_id     = Naver.client_id
  @client_secret = Naver.client_secret
  @oauth_base_uri = oauth_base_uri
  @redirect_uri = redirect_uri

  headers = { user_agent: "NAVER Ruby SDK Gem #{ Naver::Sdk::VERSION}" }
  @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.


27
28
29
30
31
32
# File 'lib/naver/oauth.rb', line 27

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.


37
38
39
40
# File 'lib/naver/oauth.rb', line 37

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.


54
55
56
57
58
# File 'lib/naver/oauth.rb', line 54

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.


46
47
48
# File 'lib/naver/oauth.rb', line 46

def extract_token
  @oauth_token.to_hash if @oauth_token
end