Skip to content
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

Using the Access Token #1374

Open
Dylan-Prins opened this issue Aug 6, 2024 · 4 comments
Open

Using the Access Token #1374

Dylan-Prins opened this issue Aug 6, 2024 · 4 comments

Comments

@Dylan-Prins
Copy link

Question

I want to use the access token generated from Add-PodeAuth in other WebPAges.
I cant find how to do this

@mdaneri
Copy link
Contributor

mdaneri commented Aug 6, 2024

Please take a look at the examples and the tutorial. You should be able to find your answer.
https://github.com/Badgerati/Pode/blob/develop/examples/web-auth-apikey-jwt.ps1

Documentation:
https://badgerati.github.io/Pode/Tutorials/Authentication/Methods/ApiKey/

@Dylan-Prins
Copy link
Author

I cant find the answer :(

I have the following code:

Add-PodeWebPage -Name 'Subscriptions' -Icon 'Settings' -ArgumentList $accesstoken -ScriptBlock {
        param($Accesstoken)

        New-PodeWebCard -Content @(
            New-PodeWebTable -Name 'Subscriptions' -ArgumentList $accesstoken -ScriptBlock {
                param($Accesstoken)
                $config = Get-PodeConfig

                $tenantid = $config.tenantId
                $Uri = "https://login.microsoftonline.com/$tenantid/oauth2/v2.0/token"
                $Headers = @{'Content-Type' = 'application/x-www-form-urlencoded' }
                $Method = 'POST'
                $Body = @{
                    grant_type          = 'urn:ietf:params:oauth:grant-type:jwt-bearer'
                    scope               = 'https://management.azure.com/.default'
                    client_id           = $config.clientId
                    client_secret       = $config.clientSecret
                    assertion           = $accessToken
                    requested_token_use	= 'on_behalf_of'
                }

                $token = (Invoke-WebRequest -Uri $Uri -Method $Method -Headers $Headers -Body $body).Content | ConvertFrom-Json -Depth 10

                $uri = "https://management.azure.com/subscriptions?api-version=2022-12-01"

                # Set up the headers, including the authorization token
                $headers = @{
                    "Authorization" = "Bearer $($token.access_token)"
                    "Content-Type"  = "application/json"
                }

                (Invoke-RestMethod -Uri $uri -Method Get -Headers $headers).value | Select-Object DisplayName, SubscriptionId, State
            }
        )
    }

I am looking for a way to not have access tokens as parameters. I guess I could save them as secret, but it is only for a session.

@mdaneri
Copy link
Contributor

mdaneri commented Aug 7, 2024

This is a Pode.Web question. You posted on the wrong forum.
It looks like Add-PodeWebPage is missing the Authentication parameter.
I suggest to post this question in Discord

@Badgerati
Copy link
Owner

Hey @Dylan-Prins,

It looks like you're using OAuth with AAD? There's a write-up of it here: https://badgerati.github.io/Pode/Tutorials/Authentication/Inbuilt/AzureAD/#full-example

The $accessToken will be available as a parameter to the scriptblock supplied to Add-PodeAuth. You can either add the access token to the $user object returned and be able to retrieve it via $WebEvent.Auth.User in your page/table. Or, you could add it to the user's session in the Add-PodeAuth script via $WebEvent.Session.Data.AccessToken = $accessToken, and the retrieve accordingly as well.

Hope that helps :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants