bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2017-2018 Dovecot authors, see the included COPYING file */
228f1e8d583b32a8ae8d192c0b9ebac95ada3308Aki Tuomimech_oauthbearer_input(struct dsasl_client *_client,
228f1e8d583b32a8ae8d192c0b9ebac95ada3308Aki Tuomi const char **error_r)
228f1e8d583b32a8ae8d192c0b9ebac95ada3308Aki Tuomi *error_r = "Server sent non-empty initial response";
228f1e8d583b32a8ae8d192c0b9ebac95ada3308Aki Tuomi /* if response is empty, authentication has *SUCCEEDED* */
228f1e8d583b32a8ae8d192c0b9ebac95ada3308Aki Tuomi /* authentication has failed, try parse status.
228f1e8d583b32a8ae8d192c0b9ebac95ada3308Aki Tuomi we are only interested in extracting status if possible
228f1e8d583b32a8ae8d192c0b9ebac95ada3308Aki Tuomi so we don't really need to much error handling. */
228f1e8d583b32a8ae8d192c0b9ebac95ada3308Aki Tuomi struct istream *is = i_stream_create_from_data(input, input_len);
228f1e8d583b32a8ae8d192c0b9ebac95ada3308Aki Tuomi struct json_parser *parser = json_parser_init(is);
228f1e8d583b32a8ae8d192c0b9ebac95ada3308Aki Tuomi while (json_parse_next(parser, &jtype, &value)>0) {
228f1e8d583b32a8ae8d192c0b9ebac95ada3308Aki Tuomi /* deinitialize json parser */
228f1e8d583b32a8ae8d192c0b9ebac95ada3308Aki Tuomi *error_r = t_strdup_printf("Error parsing JSON reply: %s",
228f1e8d583b32a8ae8d192c0b9ebac95ada3308Aki Tuomi *error_r = t_strdup_printf("Failed to authenticate: %s",
228f1e8d583b32a8ae8d192c0b9ebac95ada3308Aki Tuomimech_oauthbearer_output(struct dsasl_client *_client,
228f1e8d583b32a8ae8d192c0b9ebac95ada3308Aki Tuomi const unsigned char **output_r, size_t *output_len_r,
228f1e8d583b32a8ae8d192c0b9ebac95ada3308Aki Tuomi const char **error_r)
228f1e8d583b32a8ae8d192c0b9ebac95ada3308Aki Tuomi str_printfa(str, "n,a=%s,\x01", _client->set.authid);
228f1e8d583b32a8ae8d192c0b9ebac95ada3308Aki Tuomi if (client->host != NULL && *client->host != '\0')
228f1e8d583b32a8ae8d192c0b9ebac95ada3308Aki Tuomi str_printfa(str, "auth=Bearer %s\x01", _client->password);
228f1e8d583b32a8ae8d192c0b9ebac95ada3308Aki Tuomi const unsigned char **output_r, size_t *output_len_r,
228f1e8d583b32a8ae8d192c0b9ebac95ada3308Aki Tuomi const char **error_r)
b1808dcac22fa2c75d5666cc98c9ab59f030a798Aki Tuomi str_printfa(str, "user=%s\x01", _client->set.authid);
228f1e8d583b32a8ae8d192c0b9ebac95ada3308Aki Tuomi str_printfa(str, "auth=Bearer %s\x01", _client->password);
228f1e8d583b32a8ae8d192c0b9ebac95ada3308Aki Tuomimech_oauthbearer_set_parameter(struct dsasl_client *_client, const char *key,
228f1e8d583b32a8ae8d192c0b9ebac95ada3308Aki Tuomi } else if (net_str2port(key, &client->port) < 0) {
228f1e8d583b32a8ae8d192c0b9ebac95ada3308Aki Tuomimech_oauthbearer_get_result(struct dsasl_client *_client, const char *key,
228f1e8d583b32a8ae8d192c0b9ebac95ada3308Aki Tuomi const char **value_r, const char **error_r ATTR_UNUSED)
228f1e8d583b32a8ae8d192c0b9ebac95ada3308Aki Tuomi /* this is set to value after login attempt */
228f1e8d583b32a8ae8d192c0b9ebac95ada3308Aki Tuomiconst struct dsasl_client_mech dsasl_client_mech_oauthbearer = {
228f1e8d583b32a8ae8d192c0b9ebac95ada3308Aki Tuomi .struct_size = sizeof(struct oauthbearer_dsasl_client),
228f1e8d583b32a8ae8d192c0b9ebac95ada3308Aki Tuomiconst struct dsasl_client_mech dsasl_client_mech_xoauth2 = {
228f1e8d583b32a8ae8d192c0b9ebac95ada3308Aki Tuomi .struct_size = sizeof(struct oauthbearer_dsasl_client),