File: resetpassword.c

package info (click to toggle)
libisds 0.9-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,348 kB
  • ctags: 1,659
  • sloc: ansic: 24,898; sh: 11,772; makefile: 393; xml: 375; sed: 16
file content (145 lines) | stat: -rw-r--r-- 4,478 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#define _XOPEN_SOURCE 500
#include <stdlib.h>
#include <stdio.h>
#include <locale.h>
#include <time.h>
#include <string.h>
#include <isds.h>
#include "common.h"

void reset_password(struct isds_ctx *ctx,
        const struct isds_DbOwnerInfo *box, const struct isds_DbUserInfo *user,
        const _Bool paid,
        struct isds_credentials_delivery *credentials_delivery) {
    char *refnumber = NULL;

    printf("Resetting password\n");
    isds_error err = isds_reset_password(ctx, box, user, paid, NULL,
            credentials_delivery, &refnumber);
    if (err) {
        printf("isds_reset_password() failed: %s: %s\n",
                isds_strerror(err), isds_long_message(ctx));
    } else {
        printf("isds_reset_password() succeeded as request #%s.\n"
                "This should not happen\n", refnumber);
        if (credentials_delivery) {
            printf("email = %s\n", credentials_delivery->email);
            printf("token = %s\n", credentials_delivery->token);
            printf("new_user_name = %s\n", credentials_delivery->new_user_name);
        }
    }
    printf("\n");
    free(refnumber);
}

int main(int argc, char **argv) {
    struct isds_ctx *ctx = NULL;
    isds_error err;
    struct isds_DbOwnerInfo *db_owner_info = NULL;
    struct isds_DbUserInfo *db_user_info = NULL;
    
    setlocale(LC_ALL, "");

    err = isds_init();
    if (err) {
        printf("isds_init() failed: %s\n", isds_strerror(err));
        exit(EXIT_FAILURE);
    }

    isds_set_logging(ILF_ALL & ~ILF_HTTP, ILL_ALL);

    ctx = isds_ctx_create();
    if (!ctx) {
        printf("isds_ctx_create() failed");
    }

    err = isds_set_timeout(ctx, 10000);
    if (err) {
        printf("isds_set_timeout() failed: %s\n", isds_strerror(err));
    }

    err = isds_login(ctx, url, username(), password(), NULL, NULL);
    if (err) {
        printf("isds_login() failed: %s: %s\n", isds_strerror(err),
                isds_long_message(ctx));
    } else {
        printf("Logged in :)\n");
    }


    {
        /* Get info about my box */
        printf("Getting info about my box:\n");
        err = isds_GetOwnerInfoFromLogin(ctx, &db_owner_info);
        if (err) {
            printf("isds_GetOwnerInfoFromLogin() failed: %s: %s\n",
                    isds_strerror(err), isds_long_message(ctx));
        } else {
            printf("isds_GetOwnerInfoFromLogin() succeeded\n");
        }
        print_DbOwnerInfo(db_owner_info);
    }

    {
        /* Get info about my account */
        printf("Getting info about my account:\n");
        err = isds_GetUserInfoFromLogin(ctx, &db_user_info);
        if (err) {
            printf("isds_GetUserInfoFromLogin() failed: %s: %s\n",
                    isds_strerror(err), isds_long_message(ctx));
        } else {
            printf("isds_GetUserInfoFromLogin() succeeded\n");
            print_DbUserInfo(db_user_info);
        }
    }

    if (db_owner_info && db_user_info) {
#if 0
        struct isds_credentials_delivery credentials_delivery;
        /* Try some invalid invocation that should fail */
        reset_password(ctx, db_owner_info, db_user_info, 0, NULL);
        reset_password(ctx, db_owner_info, db_user_info, 1, NULL);

        credentials_delivery.email = NULL;
        credentials_delivery.token = NULL;
        credentials_delivery.new_user_name = NULL;
        reset_password(ctx, db_owner_info, db_user_info, 1,
                &credentials_delivery);
        free(credentials_delivery.token);
        free(credentials_delivery.new_user_name);
        
        credentials_delivery.email = "42";
        credentials_delivery.token = NULL;
        credentials_delivery.new_user_name = NULL;
        reset_password(ctx, db_owner_info, db_user_info, 1,
                &credentials_delivery);
        free(credentials_delivery.token);
        free(credentials_delivery.new_user_name);
#endif
        fprintf(stderr, "This function can lose current user credentials. "
                "DO NOT TRY IT!\n");
    }

    isds_DbOwnerInfo_free(&db_owner_info);
    isds_DbUserInfo_free(&db_user_info);


    err = isds_logout(ctx);
    if (err) {
        printf("isds_logout() failed: %s\n", isds_strerror(err));
    }


    err = isds_ctx_free(&ctx);
    if (err) {
        printf("isds_ctx_free() failed: %s\n", isds_strerror(err));
    }


    err = isds_cleanup();
    if (err) {
        printf("isds_cleanup() failed: %s\n", isds_strerror(err));
    }

    exit (EXIT_SUCCESS);
}