Class: Naver::Oauth
- Inherits:
-
Object
- Object
- Naver::Oauth
- Defined in:
- lib/naver/oauth.rb
Instance Method Summary collapse
-
#authorization_url(requested_scopes: ["public"]) ⇒ String
Get OAuth URL for user authentication and authorization.
-
#authorize!(auth_code) ⇒ Object
Generate an access token given an auth code received from NAVER.
-
#create_and_assign_token(token_extract) ⇒ OAuth2::AccessToken?
Create and assign new access token from extracted token.
-
#extract_token ⇒ Hash?
Extract hash with OAuth token attributes.
-
#initialize(redirect_uri: Naver.redirect_uri) ⇒ Oauth
constructor
Create a OAuth object.
Constructor Details
#initialize(redirect_uri: Naver.redirect_uri) ⇒ Oauth
Create a OAuth object.
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.
23 24 25 26 27 28 |
# File 'lib/naver/oauth.rb', line 23 def (requested_scopes: ["public"]) @oauth.auth_code.( 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.
33 34 35 36 |
# File 'lib/naver/oauth.rb', line 33 def (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.
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_token ⇒ Hash?
Extract hash with OAuth token attributes. Extracted token attributes can be used with create_and_assign_token to prevent the need for reauthorization.
42 43 44 |
# File 'lib/naver/oauth.rb', line 42 def extract_token @oauth_token.to_hash if @oauth_token end |