Skip to content

Commit

Permalink
Add docuementation for oauth2 and make client credentials optional
Browse files Browse the repository at this point in the history
  • Loading branch information
iwdt committed Feb 24, 2024
1 parent 767764f commit a6b78c6
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 13 deletions.
57 changes: 52 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ You can configure several options, which you pass in to the `provider` method vi
- `authorize_url` - absolute or relative URL path to the Authorization endpoint. Default: `/oauth/authorize`
- `token_url` - absolute or relative URL path to the Token endpoint. Default: `/oauth/token`
- `token_method` - HTTP method to use to request token (`:get`, `:post`, `:post_with_query_string`). Default: `:post`
- `auth_scheme` - HTTP method to use to authorize request (`:basic_auth` or `:request_body`). Default: `basic_auth`
- `auth_scheme` - HTTP method to use to authorize request (`:basic_auth` or `:request_body`). Default: `:basic_auth`
- `connection_opts` - `Hash` of connection options to pass to initialize `Faraday` with. Default: `{}`
- `max_redirects` - maximum number of redirects to follow. Default: `5`
- `raise_errors` - whether or not to raise an `OAuth2::Error` on responses with 400+ status codes. Default: `true`
Expand All @@ -98,7 +98,11 @@ In `config/initializers/omniauth.rb`
Rails.application.config.middleware.use OmniAuth::Builder do
provider :shikimori, ENV['SHIKIMORI_KEY'], ENV['SHIKIMORI_KEY'],
scope: %w[user_rates comments topics],
app_name: ENV['SHIKIMORI_APP_NAME']
app_name: ENV['SHIKIMORI_APP_NAME'],
client_options: {
redirect_url: 'https://my-awesome-site.example/auth/shikimori/callback',
logger: Rails.logger
}
end
```

Expand All @@ -109,7 +113,10 @@ Add middleware to your rack-based application:
use OmniAuth::Builder do
provider :shikimori, ENV['SHIKIMORI_KEY'], ENV['SHIKIMORI_SECRET'],
scope: %w[user_rates comments topics],
app_name: ENV['SHIKIMORI_APP_NAME']
app_name: ENV['SHIKIMORI_APP_NAME'],
client_options: {
redirect_url: 'https://my-awesome-site.example/auth/shikimori/callback'
}
end
```

Expand Down Expand Up @@ -142,11 +149,51 @@ require 'shikimori-oauth2'

### Configuration

TODO
While `Shikimori::OAuth2::Client` accepts a range of options when creating a new client instance, Shikimori's configuration API allows you to set your configuration options at the module level. This is particularly handy if you're creating a number of client instances based on some shared defaults. Changing options affects new instances only and will not modify existing `Shikimori::OAuth2::Client` instances created with previous options.

Configuring module defaults

Every writable attribute in `Shikimori::OAuth2::Config` can be set in batch:

```ruby
Shikimori::Oauth2.configure do |c|
c.site = 'https://shikimori.one/'
c.app_name = 'My awesome site'
c.options = {
redirect_uri: 'https://my-awesome-site.example/auth/shikimori/callback',
authorize_url: '/oauth/authorize',
token_url: '/oauth/token',
token_method: :post,
auth_scheme: :basic_auth,
connection_opts: {},
max_redirects: 5,
raise_errors: true,
logger: ::Logger.new($stdout),
}
end
```

Also aviable global options from [OAuth2 gem](https://gitlab.com/oauth-xx/oauth2#global-configuration).

### Usage

TODO
To get access and refresh tokens, follow this example:

```ruby
require 'shikimori-oauth2'

client = Shikimori::OAuth2::Client.new('client_id', 'client_secret', app_name: 'Api test')
client.auth_code.authorize_url(scope: 'user_rates+comments+topics', redirect_uri: 'urn:ietf:wg:oauth:2.0:oob')
#=> https://shikimori.one/oauth/authorize?client_id=client_id&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&scope=user_rates+comments+topics
#=> Open this link at browser and copy code

access = client.auth_code.get_token('authorization_code_value', redirect_uri: 'urn:ietf:wg:oauth:2.0:oob')

access.token #=> Access token
access.refresh_token #=> Refresh token
access.expires_in #=> 86400
access.expires_at #=> 1708887613
```

## Shikimori API

Expand Down
2 changes: 1 addition & 1 deletion lib/shikimori/api/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Client

attr_reader :v1, :v2

def initialize(site = DEFAULT_SITE_URL, app_name:, access_token:, refresh_token:)
def initialize(site = DEFAULT_SITE_URL, app_name: nil, access_token: nil, refresh_token: nil)
rest = REST.new(
access_token: access_token,
refresh_token: refresh_token,
Expand Down
4 changes: 2 additions & 2 deletions lib/shikimori/api/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Shikimori
module API
# Helpers to make requests
class REST
def initialize(app_name:, access_token:, refresh_token:)
def initialize(app_name: nil, access_token: nil, refresh_token: nil)
@app_name = app_name
@access_token = access_token
@refresh_token = refresh_token
Expand Down Expand Up @@ -100,7 +100,7 @@ def headers_from(options)
'Content-Type' => 'application/json',
'Authorization' => "Bearer #{@access_token}",
'User-Agent' => @app_name
)
).compact
end

def query_params_from(uri, options)
Expand Down
2 changes: 1 addition & 1 deletion sig/shikimori/api/client.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Shikimori
attr_reader v1: V1
attr_reader v2: V2

def initialize: (?String site, app_name: String, access_token: String, refresh_token: String) -> void
def initialize: (?String site, ?app_name: String | nil, ?access_token: String | nil, ?refresh_token: String | nil) -> void
end
end
end
8 changes: 4 additions & 4 deletions sig/shikimori/api/rest.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ module Shikimori
type request = Net::HTTP::Delete | Net::HTTP::Get | Net::HTTP::Post | Net::HTTP::Put
type body = Hash[_ToS, Object] | nil

@app_name: String
@access_token: String
@refresh_token: String
@app_name: String | nil
@access_token: String | nil
@refresh_token: String | nil

def initialize: (app_name: String, access_token: String, refresh_token: String) -> void
def initialize: (?app_name: String | nil, ?access_token: String | nil, ?refresh_token: String | nil) -> void

def get: (::URI::Generic, **untyped) -> untyped
def post: (::URI::Generic, Hash[_ToS, untyped] data, **untyped) -> untyped
Expand Down

0 comments on commit a6b78c6

Please sign in to comment.