-
Notifications
You must be signed in to change notification settings - Fork 10
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
The scopes problem #5
Comments
I believe the only solution would be to add a new method Obviously this would be an optional method available only on extractors for OAuth2 services who supports scopes. We could also create a specific interface such as What do you think about it? Can you help me with the development of this feature? |
I can try something out tomorrow. Your blog post and extended documentation are really helpful 👍 |
@cmfcmf, nice to know you found it useful 😉 |
/**
* Interface ScopeAwareExtractorInterface
*
* @package OAuth\UserData\Extractor
*/
interface ScopeAwareExtractorInterface
{
/**
* Get a map of fields mapped to the required scopes.
*
* @return array
*/
public function getFieldScopesMap();
} That's what I have now, but the Interface might not be enough. I imagine a method being available for each ScopeAwareExtractor which looks like this, but don't know where to place it. /**
* Returns all scopes needed for the specified fields.
* @param mixed $argument, ... All the fields you need to fetch.
*
* @return array All the scopes needed for the required fields.
*/
public function getScopesForFields()
{
$fields = func_get_args();
$scopesNeeded = array();
$fieldScopesMap = $this->getFieldScopesMap();
foreach ($fields as $field) {
if ($fieldScopesMap[$field] !== null && !in_array($fieldScopesMap[$field], $scopesNeeded)) {
$scopesNeeded[] = $fieldScopesMap[$field];
}
}
return $scopesNeeded;
} |
Great idea. I really like the method I am thinking about where to place it: I feel it would be something that should be implemented as a Trait, but, obviously, I don't think it's a good idea to raise the minimum PHP version to 5.4. If we move it to the We can even think about creating two new classes Another solution, maybe the most viable, could be to create a generic static function in the public static function extractMappedValues($map, $fields, $removeDuplicates = true)
{
//...
} then the function public function getScopesForFields()
{
return ArrayUtils::extractMappedValues($this->getFieldScopesMap(), func_get_args());
} This would reduce a lot the code duplication issue and we can move the method |
Uhh, we're running into a chicken-egg problem: The place where scopes are passed to the service is right when instantiating the service:
— https://github.com/Lusitanian/PHPoAuthLib/blob/master/examples/github.php#L35 But to instantiate the Extractor (which would have to be done before creating the service, to pass the scopes), the service must've already been created:
|
Yep... I was just thinking about this possible issue and considering if the scope related methods should be static... $gitHub = $serviceFactory->createService(
'GitHub',
$credentials,
$storage,
Github::getScopesForFields(
Github::FIELD_USERNAME,
Github::FIELD_UNIQUE_ID
)
); |
Hmm, this doesn't help much in my use case. I'm doing it like so now: // echo $this->getOAuthServiceName()
// can be 'Twitter', 'GitHub', etc.
$serviceStub = $serviceFactory->createService($this->getOAuthServiceName(), $credentials, $storage);
$extractorFactory = new ExtractorFactory();
$extractor = $extractorFactory->get($serviceStub);
unset($serviceStub);
if ($extractor instanceof ScopeAwareExtractorInterface && $extractor instanceof LazyExtractor) {
if (!$extractor->supportsUniqueId()) {
throw new \RuntimeException('Every provider must support a unique id!');
}
$fields = array($extractor::FIELD_UNIQUE_ID);
if ($forRegistration) {
/** @todo Language */
if ($extractor->supportsUsername()) {
$fields[] = $extractor::FIELD_USERNAME;
}
if ($extractor->supportsEmail()) {
$fields[] = $extractor::FIELD_EMAIL;
}
if ($extractor->supportsVerifiedEmail()) {
$fields[] = $extractor::FIELD_VERIFIED_EMAIL;
}
}
$scopes = $extractor->getScopesForFields($fields);
} else {
$scopes = array();
}
$this->service = $serviceFactory->createService($this->getOAuthServiceName(), $credentials, $storage, $scopes);
return $this->service; As you see, I need to call the I'll open a work in progress PR to let you see the changes instead of me only explaining what I did. |
Yes, I see we should find a way to simplify the interface and reduce the amount of code needed to perform what you're trying to do (that's a common use case IMHO). Thanks for the effort. I'll follow your work on the fork. |
I still see scopes as a problematic point. If I have to look up the scopes needed for certain information in the provider's documentation, then it's pretty easy to look up the name of the field containing the required information too. And looking up stuff in the provider's documentation is exactly what this library should try to handle for the user, no?
The text was updated successfully, but these errors were encountered: