Skip to content

Add type restrictions to Oauth directory #15687

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/oauth/authorization_header.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ struct OAuth::AuthorizationHeader
@first = true
end

def add(key, value)
def add(key : String, value : String?) : Nil
return unless value

@str << ", " unless @first
Expand Down
6 changes: 3 additions & 3 deletions src/oauth/consumer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class OAuth::Consumer
# [RFC 5849, Section 2.1](https://tools.ietf.org/html/rfc5849#section-2.1).
#
# Raises `OAuth::Error` if there was an error getting the request token.
def get_request_token(oauth_callback = "oob") # : RequestToken
def get_request_token(oauth_callback : String = "oob") : OAuth::RequestToken
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Full path is not needed.

Suggested change
def get_request_token(oauth_callback : String = "oob") : OAuth::RequestToken
def get_request_token(oauth_callback : String = "oob") : RequestToken

post(nil, nil, {"oauth_callback" => oauth_callback}, @request_token_uri) do |response|
RequestToken.from_response(response.body)
end
Expand All @@ -81,7 +81,7 @@ class OAuth::Consumer
# Returns an authorize URI from a given request token to redirect the user
# to obtain an access token, as specified by
# [RFC 5849, Section 2.2](https://tools.ietf.org/html/rfc5849#section-2.2).
def get_authorize_uri(request_token, oauth_callback = nil) : String
def get_authorize_uri(request_token : OAuth::RequestToken, oauth_callback : String? = nil) : String
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Suggested change
def get_authorize_uri(request_token : OAuth::RequestToken, oauth_callback : String? = nil) : String
def get_authorize_uri(request_token : RequestToken, oauth_callback : String? = nil) : String

get_authorize_uri(request_token, oauth_callback) { }
end

Expand Down Expand Up @@ -116,7 +116,7 @@ class OAuth::Consumer
# [RFC 5849, Section 2.3](https://tools.ietf.org/html/rfc5849#section-2.3).
#
# Raises `OAuth::Error` if there was an error getting the access token.
def get_access_token(request_token, oauth_verifier, extra_params = nil) : AccessToken
def get_access_token(request_token : OAuth::RequestToken, oauth_verifier : String, extra_params : Hash(String, String)? = nil) : AccessToken
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Suggested change
def get_access_token(request_token : OAuth::RequestToken, oauth_verifier : String, extra_params : Hash(String, String)? = nil) : AccessToken
def get_access_token(request_token : RequestToken, oauth_verifier : String, extra_params : Hash(String, String)? = nil) : AccessToken

extra_params ||= {} of String => String
extra_params["oauth_verifier"] = oauth_verifier
post(request_token.token, request_token.secret, extra_params, @access_token_uri) do |response|
Expand Down
2 changes: 1 addition & 1 deletion src/oauth/oauth.cr
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
module OAuth
# Sets up an `HTTP::Client` to add an OAuth authorization header to every request performed.
# Check this module's docs for an example usage.
def self.authenticate(client : HTTP::Client, token, token_secret, consumer_key, consumer_secret, extra_params = nil) : Nil
def self.authenticate(client : HTTP::Client, token : String?, token_secret : String?, consumer_key : String, consumer_secret : String, extra_params : Hash(String, String)? = nil) : Nil
client.before_request do |request|
authenticate client, request, token, token_secret, consumer_key, consumer_secret, extra_params
end
Expand Down
4 changes: 2 additions & 2 deletions src/oauth/params.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ struct OAuth::Params
@params = [] of {String, String}
end

def add(key, value) : Nil
def add(key : String, value : String?) : Nil
if value
@params << {URI.encode_www_form(key, space_to_plus: false), URI.encode_www_form(value, space_to_plus: false)}
end
end

def add_query(query) : Nil
def add_query(query : String) : Nil
URI::Params.parse(query) do |key, value|
add key, value
end
Expand Down
2 changes: 1 addition & 1 deletion src/oauth/request_token.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class OAuth::RequestToken
def initialize(@token : String, @secret : String)
end

def self.from_response(response) : self
def self.from_response(response : String) : self
token = nil
secret = nil

Expand Down
6 changes: 3 additions & 3 deletions src/oauth/signature.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ struct OAuth::Signature
def initialize(@consumer_key : String, @client_shared_secret : String, @oauth_token : String? = nil, @token_shared_secret : String? = nil, @extra_params : Hash(String, String)? = nil)
end

def base_string(request, tls, ts, nonce) : String
def base_string(request : HTTP::Request, tls, ts : String, nonce : String) : String
base_string request, tls, gather_params(request, ts, nonce)
end

Expand All @@ -17,12 +17,12 @@ struct OAuth::Signature
end
end

def compute(request, tls, ts, nonce) : String
def compute(request : HTTP::Request, tls, ts : String, nonce : String) : String
base_string = base_string(request, tls, ts, nonce)
Base64.strict_encode(OpenSSL::HMAC.digest :sha1, key, base_string)
end

def authorization_header(request, tls, ts, nonce) : String
def authorization_header(request : HTTP::Request, tls, ts : String, nonce : String) : String
oauth_signature = compute request, tls, ts, nonce

auth_header = AuthorizationHeader.new
Expand Down
2 changes: 1 addition & 1 deletion src/oauth2/access_token/access_token.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# Use `#authenticate` to authenticate an `HTTP::Client`.
abstract class OAuth2::AccessToken
def self.new(pull : JSON::PullParser)
def self.new(pull : JSON::PullParser) : self
token_type = nil
access_token = nil
expires_in = nil
Expand Down
2 changes: 1 addition & 1 deletion src/oauth2/access_token/bearer.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "./access_token"

class OAuth2::AccessToken::Bearer < OAuth2::AccessToken
def self.new(pull : JSON::PullParser)
def self.new(pull : JSON::PullParser) : self
OAuth2::AccessToken.new(pull).as(self)
end

Expand Down
6 changes: 3 additions & 3 deletions src/oauth2/access_token/mac.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ require "base64"
require "./access_token"

class OAuth2::AccessToken::Mac < OAuth2::AccessToken
def self.new(pull : JSON::PullParser)
def self.new(pull : JSON::PullParser) : OAuth2::AccessToken::Mac
OAuth2::AccessToken.new(pull).as(self)
end

property mac_algorithm : String
property mac_key : String
property issued_at : Int64

def initialize(access_token, expires_in, @mac_algorithm, @mac_key, refresh_token = nil, scope = nil, @issued_at = Time.utc.to_unix, extra = nil)
def initialize(access_token : String, expires_in, @mac_algorithm : String, @mac_key : String, refresh_token : String? = nil, scope : String? = nil, @issued_at : Int64 = Time.utc.to_unix, extra : Hash(String, String)? = nil)
super(access_token, expires_in, refresh_token, scope, extra)
end

Expand All @@ -34,7 +34,7 @@ class OAuth2::AccessToken::Mac < OAuth2::AccessToken
request.headers["Authorization"] = header
end

def self.signature(ts, nonce, method, uri, host, port, ext, mac_algorithm, mac_key) : String
def self.signature(ts, nonce : String, method : String, uri : String, host : String, port : Int32 | String, ext : String, mac_algorithm : String, mac_key : String) : String
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String restriction for port seems a bit odd...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha, agreed, good catch. Looks to be caused by this spec that's passing in "4000" for the port

normalized_request_string = "#{ts}\n#{nonce}\n#{method}\n#{uri}\n#{host}\n#{port}\n#{ext}\n"

digest = case mac_algorithm
Expand Down
8 changes: 4 additions & 4 deletions src/oauth2/client.cr
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class OAuth2::Client

# Builds an authorize URI, as specified by
# [RFC 6749, Section 4.1.1](https://tools.ietf.org/html/rfc6749#section-4.1.1).
def get_authorize_uri(scope = nil, state = nil) : String
def get_authorize_uri(scope : String? = nil, state : String? = nil) : String
get_authorize_uri(scope, state) { }
end

Expand Down Expand Up @@ -139,7 +139,7 @@ class OAuth2::Client

# Gets an access token using the resource owner credentials, as specified by
# [RFC 6749, Section 4.3.2](https://tools.ietf.org/html/rfc6749#section-4.3.2).
def get_access_token_using_resource_owner_credentials(username : String, password : String, scope = nil) : AccessToken
def get_access_token_using_resource_owner_credentials(username : String, password : String, scope : String? = nil) : AccessToken
get_access_token do |form|
form.add("grant_type", "password")
form.add("username", username)
Expand All @@ -150,7 +150,7 @@ class OAuth2::Client

# Gets an access token using client credentials, as specified by
# [RFC 6749, Section 4.4.2](https://tools.ietf.org/html/rfc6749#section-4.4.2).
def get_access_token_using_client_credentials(scope = nil) : AccessToken
def get_access_token_using_client_credentials(scope : String? = nil) : AccessToken
get_access_token do |form|
form.add("grant_type", "client_credentials")
form.add("scope", scope) unless scope.nil?
Expand All @@ -159,7 +159,7 @@ class OAuth2::Client

# Gets an access token using a refresh token, as specified by
# [RFC 6749, Section 6](https://tools.ietf.org/html/rfc6749#section-6).
def get_access_token_using_refresh_token(refresh_token, scope = nil) : AccessToken
def get_access_token_using_refresh_token(refresh_token : String?, scope : String? = nil) : AccessToken
get_access_token do |form|
form.add("grant_type", "refresh_token")
form.add("refresh_token", refresh_token)
Expand Down
2 changes: 1 addition & 1 deletion src/oauth2/session.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class OAuth2::Session
# Authenticates an `HTTP::Client`, refreshing the access token if it is expired.
#
# Invoke this method on an `HTTP::Client` before executing an HTTP request.
def authenticate(http_client) : Nil
def authenticate(http_client : HTTP::Client) : Nil
check_refresh_token
@access_token.authenticate http_client
end
Expand Down