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

Need to pass API-Key dynamically through APIPlugins in iOS #3790

Closed
Santosh2332 opened this issue Jul 23, 2024 · 7 comments
Closed

Need to pass API-Key dynamically through APIPlugins in iOS #3790

Santosh2332 opened this issue Jul 23, 2024 · 7 comments
Labels
api Issues related to the API category question General question

Comments

@Santosh2332
Copy link

Santosh2332 commented Jul 23, 2024

Is your feature request related to a problem? Please describe.

I have added awsconfiguration.json file in my project for configuration, now i need to pass API Key dynamically to configure amplify like below code(OIDC Auth)

class MyAPIAuthProviderFactory: APIAuthProviderFactory {
    let myAuthProvider = MyOIDCAuthProvider()

    override func oidcAuthProvider() -> AmplifyOIDCAuthProvider? {
        return myAuthProvider
    }
}

class MyOIDCAuthProvider : AmplifyOIDCAuthProvider {
    func getLatestAuthToken() async throws -> String {
       ....
    }
}

try Amplify.add(plugin: AWSAPIPlugin(apiAuthProviderFactory: MyAPIAuthProviderFactory()))

Is there Any way to configure API-Key from my end like above example.

Describe the solution you'd like

Need to configure API-Key from project Swift class

Describe alternatives you've considered

Provide any alternative from your end

Is the feature request related to any of the existing Amplify categories?

API

Additional context

No response

@ruisebas ruisebas added the feature-request Request a new feature label Jul 23, 2024
Copy link
Contributor

This has been identified as a feature request. If this feature is important to you, we strongly encourage you to give a 👍 reaction on the request. This helps us prioritize new features most important to you. Thank you!

@Santosh2332
Copy link
Author

If it a feature request, can i aspect any solution from your end please. I need to implement it in my current project.

@ruisebas
Copy link
Member

ruisebas commented Jul 24, 2024

Hi @Santosh2332, thanks for opening this.

I'm a bit confused by what you're describing. API Key and OpenID Connect (OIDC) are two separate and independent Authorization Types supported by AppSync.

Could you please clarify your use case, or what are you trying to achieve?


Perhaps unrelated, but you've mentioned adding the awsconfiguration.json file to your project for configuration.
Keep in mind that Amplify does not use that file, it uses amplifyconfiguration.json for Gen1, and amplify_outputs.json for Gen2.

@ruisebas ruisebas added question General question api Issues related to the API category and removed feature-request Request a new feature labels Jul 24, 2024
@Santosh2332
Copy link
Author

Santosh2332 commented Jul 25, 2024

Hi @ruisebas thanks for the reply.

I am using API_Key as authorization type for configuration that is configure in amplifyconfiguration.json file.
Now my requirement is to pass api key at run time, through plugin or any custome configuration, is it possible, if yes then please provide me the way.

@ruisebas
Copy link
Member

If you're relying on the amplifyconfiguration.json file to configure Amplify, then you cannot just overwrite AWSAPIPlugin's apiKey value.

What you can do is create your own AmplifyConfiguration instance and set the key in there. However you will need to replicate the configuration for all the plugins your use.

Here's a simple example:

let apiKey: JSONValue = "YOUR API KEY"
let configuration = AmplifyConfiguration(
    api: .init(plugins: [
        "awsAPIPlugin" : [
            "[API NAME]": [
                "endpointType": "ENDPOINT TYPE]",
                "endpoint": "[ENDPOINT URL]",
                "region": "[REGION]",
                "authorizationType": "API_KEY",
                "apiKey": apiKey
            ]
        ]
    ]),
    // Add the remaining categories...
)

try Amplify.add(plugin: AWSAPIPlugin())
// Add the remaining plugins 

try Amplify.configure(configuration)

Alternatively, if you don't want to manually recreate the whole file because you have lots of plugins and/or properties, you can just parse the amplifyconfiguration.json into your own type, edit the corresponding api section and then create an updated AmplifyConfiguration instance:

struct MyConfiguration: Decodable {
    // You can omit categories that you don't use
    var analytics: AnalyticsCategoryConfiguration?
    var api: APICategoryConfiguration?
    var auth: AuthCategoryConfiguration?
    var dataStore: DataStoreCategoryConfiguration?
    var geo: GeoCategoryConfiguration?
    var hub: HubCategoryConfiguration?
    var logging: LoggingCategoryConfiguration?
    var notifications: NotificationsCategoryConfiguration?
    var predictions: PredictionsCategoryConfiguration?
    var storage: StorageCategoryConfiguration?

    init() throws {
        guard let url = Bundle.main.url(forResource: "amplifyconfiguration", withExtension: "json") else {
            throw ConfigurationError.invalidAmplifyConfigurationFile(
                "Unable to read amplifyconfiguration.json",
                "Make sure the file is present in the bundle"
            )
        }
        let data = try Data(contentsOf: url)
        self = try JSONDecoder().decode(MyConfiguration.self, from: data)
    }

    func createAmplifyConfiguration() -> AmplifyConfiguration {
        return .init(
            analytics: analytics,
            api: api,
            auth: auth,
            dataStore: dataStore,
            geo: geo,
            hub: hub,
            logging: logging,
            notifications: notifications,
            predictions: predictions,
            storage: storage
        )
    }
}
let apiKey: JSONValue = "YOUR API KEY"
var configuration = try MyConfiguration()
// Overwrite only the API category configuration, there's no need to do anything for the remaining ones
configuration.api = .init(plugins: [
    "awsAPIPlugin" : [
        "[API NAME]": [
            "endpointType": "ENDPOINT TYPE]",
            "endpoint": "[ENDPOINT URL]",
            "region": "[REGION]",
            "authorizationType": "API_KEY",
            "apiKey": apiKey
        ]
    ]
])

try Amplify.add(plugin: AWSAPIPlugin())
// Add the remaining plugins 
try Amplify.configure(configuration.createAmplifyConfiguration())

@Santosh2332
Copy link
Author

@ruisebas Thank you.

Copy link
Contributor

This issue is now closed. Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Issues related to the API category question General question
Projects
None yet
Development

No branches or pull requests

2 participants