Closed shanmugharajk closed 5 years ago
@shanmugharajk what version of Guardian are you using?
Mix config info
{:guardian, "~> 1.0"}
I did some minor debugging into this. You can get it working if any of:
1) available_permissions
type relaxed to map
2) available_permissions
type inlined to %{optional(atom) => [atom]}
3) available_permissions
spec removed
4) Call to Guardian.Permissions.Bitwise.available_from_normalized(@normalized_perms)
inlined into available_permissions
.
These all have pretty self evident flaws.
Adding | [atom]
to the type of permission_set
did not work as expected. I'm very confused why this is failing.
Is it possible to suppress these warnings in my code?
Hi,
I am also getting dailyzer issue when running mix dialyzer
, I wonder is there any progress on this issue.
@blisscs I don't think so since I just knew about it.
PR welcome 💜
Running mix dialyzer on current master branch results in no errors. @blisscs @shanmugharajk could you verify this?
Hey folks, feel free to reopen the issue, I believe that this should be fixed already. v2
release is coming.
Thank you @yordis and @Hanspagh for the updates.
I am currently using "~> 1.0" now, I am seeing that I need to do some other changes to use current master version here.
Will keep my version as it is for now. Once I try it will update here one again.
Best
@Hanspagh Yes, verified and the warnings are gone! Its fine now.
Sorry again, I got another problem now this is my config
issuer: "epos",
secret_key: "+rENf7iolLX1ySSwS6smLJ+p/5ArJsI8DAgjhuCUMBFszV9aul8SO4jncpcfhS1x",
permissions: %{
basic_user: [
:sales,
:stock
],
super_admin: [
:all_access
],
admin: [
:sales,
:stock,
:products,
:expense_write,
:receivings_write
]
}
and when i call this use Guardian.Permissions.Bitwise
like this
defmodule Epos.Guardian do
use(Guardian,
otp_app: :epos
)
use Guardian.Permissions.Bitwise
def subject_for_token(%{} = user, _claims) do
# You can use any value for the subject of your token but
# it should be useful in retrieving the resource later, see
# how it being used on `resource_from_claims/1` function.
# A unique `id` is a good subject, a non-unique email address
# is a poor subject.
sub = to_string(user.users_id)
{:ok, sub}
end
def subject_for_token(_, _) do
{:error, :reason_for_error}
end
def resource_from_claims(%{} = claims) do
# Here we'll look up our resource from the claims, the subject can be
# found in the `"sub"` key. In `above subject_for_token/2` we returned
# the resource id so here we'll rely on that to look it up.
id = claims["sub"]
resource = Epos.Accounts.get_user(id)
{:ok, resource}
end
def resource_from_claims(_claims) do
{:error, :reason_for_error}
end
def build_claims(claims, _resource, opts) do
claims =
claims
|> encode_permissions_into_claims!(Keyword.get(opts, :permissions))
{:ok, claims}
end
end
I am getting error says
Invalid type specification for function 'Elixir.Epos.Guardian':available_permissions/0. The success typing is () -> #{'admin':=['expense_write' | 'products' | 'receivings_write' | 'sales' | 'stock',...], 'basic_user':=['sales' | 'stock',...], 'super_admin':=['all_access',...]}
I am not sure I can ask this here or stack overflow. If this is not right here please ignore this.
What I tried to get rid of the initial problem is updated from 1.0 to {:guardian, "~> 1.2"}
mix clean
mix deps.get
mix compile
Now the previous errors were gone and got the above said one 😢
Is that the whole message?
yes thats the whole message I am getting in the vscode console.
@shanmugharajk there is an incoming change that touches a lot in Permissions
module so, I will wait until that is out and then figure out if this still an issue.
Please, give us some time to prepare v2
release and let's see if the issue persist.
if you still have some problems and warnings with dialyzer this is how I solved it. I changed the original code offered for "Create a module that uses Guardian"
original code:
defmodule MyApp.Guardian do
use Guardian, otp_app: :my_app
def subject_for_token(resource, _claims) do
# You can use any value for the subject of your token but
# it should be useful in retrieving the resource later, see
# how it being used on `resource_from_claims/1` function.
# A unique `id` is a good subject, a non-unique email address
# is a poor subject.
sub = to_string(resource.id)
{:ok, sub}
end
def subject_for_token(_, _) do
{:error, :reason_for_error}
end
def resource_from_claims(claims) do
# Here we'll look up our resource from the claims, the subject can be
# found in the `"sub"` key. In `above subject_for_token/2` we returned
# the resource id so here we'll rely on that to look it up.
id = claims["sub"]
resource = MyApp.get_resource_by_id(id)
{:ok, resource}
end
def resource_from_claims(_claims) do
{:error, :reason_for_error}
end
end`
**Changed code:**
defmodule MyApp.Guardian do
use Guardian, otp_app: :my_app
def subject_for_token(resource, _claims) do
# You can use any value for the subject of your token but
# it should be useful in retrieving the resource later, see
# how it being used on `resource_from_claims/1` function.
# A unique `id` is a good subject, a non-unique email address
# is a poor subject.
sub = to_string(resource.id)
if sub do
{:ok, sub}
else
{:error, :reason_for_error}
end
end
def resource_from_claims(claims) do
# Here we'll look up our resource from the claims, the subject can be
# found in the `"sub"` key. In `above subject_for_token/2` we returned
# the resource id so here we'll rely on that to look it up.
id = claims["sub"]
resource = MyApp.get_resource_by_id(id)
if resource do
{:ok, resource}
else
{:error, :reason_for_error}
end
end
end
After you change your code, dialyzer should stop showing any errors or warnings.
Hope this helps
I am using the vs-code extension https://marketplace.visualstudio.com/items?itemName=JakeBecker.elixir-ls
My Guardian module code is as
The moment I include this use Guardian.Permissions.Bitwise i get the following warnings in vscode
I tried googling but I am failed to figure the reason for this. Could somebody please help me on this?