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

Add option to encode a string url-safe with to_base64 #384

Closed
maipet opened this issue Nov 7, 2024 · 10 comments · Fixed by #386
Closed

Add option to encode a string url-safe with to_base64 #384

maipet opened this issue Nov 7, 2024 · 10 comments · Fixed by #386
Assignees
Labels
Enhancement New feature or request

Comments

@maipet
Copy link
Collaborator

maipet commented Nov 7, 2024

In context of OERSI we need to encode a string URL-safe to base64 (Base64url format) to reuse it later in an URL.

https://www.youtube.com/watch?v=daLgsPSvD9A

to_base64 => aHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g/dj1kYUxnc1BTdkQ5QQ==

but url-safe would be: aHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g_dj1kYUxnc1BTdkQ5QQ==

To enable url-safe base64 there should be an option in the to_base64 function.

like this:
to_base64("targetElement", url-safe:"true")

@TobiasNx
Copy link
Collaborator

TobiasNx commented Nov 7, 2024

https://docs.oracle.com/javase/8/docs/api/java/util/Base64.html

I am not sure how to adjust the code to change the function by option url-safe:"true" to getUrlEncoder()

@TobiasNx
Copy link
Collaborator

TobiasNx commented Nov 7, 2024

to_base64 {
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
record.transform(params.get(0), s -> Base64.getEncoder().encodeToString(s.getBytes()));
}
},

@dr0i could you help? you introduced options to url_encode too. Maybe you have an idea. This should be introduced before the next release.

@TobiasNx TobiasNx changed the title Add function to encode url to base64 Add option to encode url to base64 Nov 7, 2024
@TobiasNx TobiasNx moved this to Selected in Metafacture Nov 7, 2024
@TobiasNx TobiasNx assigned dr0i and unassigned TobiasNx Nov 7, 2024
@TobiasNx TobiasNx added the Enhancement New feature or request label Nov 7, 2024
@dr0i
Copy link
Member

dr0i commented Nov 7, 2024

Hm, not sure where your problem is. You can extract (using regex or so) the part of the URL you need to base64, base64 that String, and replace the substring in the URL with itt. If this is what you want?

@TobiasNx
Copy link
Collaborator

TobiasNx commented Nov 7, 2024

Hm, not sure where your problem is. You can extract (using regex or so) the part of the URL you need to base64, base64 that String, and replace the substring in the URL with itt. If this is what you want?

Nope, that is not what I needed.
There is a specific way to encode strings url-safe to base64, this should be an option for the to_base64 function called e.g. url_safe

@TobiasNx TobiasNx changed the title Add option to encode url to base64 Add option to encode a string url-safe to base64 Nov 7, 2024
@TobiasNx
Copy link
Collaborator

TobiasNx commented Nov 7, 2024

But this is exactly what the Fix-function to_base64 already does, no ? : metafacture.org/playground?flux=inputFile%0A%7Copen-file%0A%7Cas-records%0A%7Cdecode-json%0A%7Cfix%28transformationFile%29%0A%7Cencode-json%28prettyPrinting%3D%22true%22%29%0A%7Cprint%0A%3B%0A&transformation=%23+Transforms+the+value+to+base64+hash%0Ato_base64%28%22key%22%29%0A&data=%7B%0A++%22key%22+%3A+%22https%3A//www.youtube.com/watch%3Fv%3DdaLgsPSvD9A%22%0A%7D%0A

output is:

 {
  "key" : "aHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g/dj1kYUxnc1BTdkQ5QQ=="
}

url-safe should be:

{
"key" : "aHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g_dj1kYUxnc1BTdkQ5QQ=="
}

See the underscore

@dr0i
Copy link
Member

dr0i commented Nov 7, 2024

Ah: you don't mean:

but url-safe would be: aHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g_dj1kYUxnc1BTdkQ5QQ==

but you mean:
"but url-safe should be: aHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g_dj1kYUxnc1BTdkQ5QQ=="

Mind 'would vs should.

@TobiasNx
Copy link
Collaborator

TobiasNx commented Nov 7, 2024

yes, exactly we would need optionally java.util.Base64.getUrlEncoder() instead of java.util.Base64.getEncoder() for that in here:

to_base64 {
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
record.transform(params.get(0), s -> Base64.getEncoder().encodeToString(s.getBytes()));
}
},

that we can do this:
to_base64("targetElement", url-safe:"true")

default url-safe:"false"

@TobiasNx TobiasNx changed the title Add option to encode a string url-safe to base64 Add option to encode a string url-safe with to_base64 Nov 7, 2024
@dr0i
Copy link
Member

dr0i commented Nov 7, 2024

Yeah, no, not instead_of , but as an option, as you said. It's not an either ... or conjunction.

@TobiasNx
Copy link
Collaborator

TobiasNx commented Nov 7, 2024

Yeah, no, not instead_of , but as an option, as you said. It's not an either ... or conjunction.

I added the adverb "optionally" and "for that" :)

I do not know how to write the code that when the option url-safe:"true" the method to_base64 utilizes java.util.Base64.getUrlEncoder() otherwise it uses java.util.Base64.getEncoder().

dr0i added a commit that referenced this issue Nov 14, 2024
@dr0i dr0i closed this as completed in #386 Nov 15, 2024
@github-project-automation github-project-automation bot moved this from Working to Done in Metafacture Nov 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement New feature or request
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants