|
8 | 8 | "io" |
9 | 9 | "net/http" |
10 | 10 | "net/url" |
| 11 | + "strings" |
11 | 12 | "time" |
12 | 13 | "vc/pkg/openid4vci" |
13 | 14 | "vc/pkg/openid4vp" |
@@ -123,29 +124,49 @@ func (c *Client) SVGTemplateReply(ctx context.Context, req *SVGTemplateRequest) |
123 | 124 |
|
124 | 125 | c.log.Debug("SVG template not available in cache, fetching from origin") |
125 | 126 |
|
126 | | - httpReq, err := http.NewRequestWithContext(ctx, http.MethodGet, svgTemplateURI, nil) |
127 | | - if err != nil { |
128 | | - return nil, err |
129 | | - } |
130 | | - |
131 | | - response, err := http.DefaultClient.Do(httpReq) |
132 | | - if err != nil { |
133 | | - return nil, err |
134 | | - } |
135 | | - defer response.Body.Close() |
136 | | - |
137 | | - if response.StatusCode != http.StatusOK { |
138 | | - err := errors.New("non ok response code from svg template origin") |
139 | | - return nil, err |
| 127 | + var template string |
| 128 | + |
| 129 | + if strings.HasPrefix(svgTemplateURI, "data:") { |
| 130 | + // Handle data: URIs (e.g., data:image/svg+xml;base64,...) |
| 131 | + commaIdx := strings.Index(svgTemplateURI, ",") |
| 132 | + if commaIdx < 0 { |
| 133 | + return nil, errors.New("invalid data URI: missing comma separator") |
| 134 | + } |
| 135 | + header := svgTemplateURI[5:commaIdx] // after "data:" |
| 136 | + data := svgTemplateURI[commaIdx+1:] |
| 137 | + |
| 138 | + if strings.HasSuffix(header, ";base64") { |
| 139 | + // Data is already base64-encoded |
| 140 | + template = data |
| 141 | + } else { |
| 142 | + // Plain text data URI — base64-encode it |
| 143 | + template = base64.StdEncoding.EncodeToString([]byte(data)) |
| 144 | + } |
| 145 | + } else { |
| 146 | + httpReq, err := http.NewRequestWithContext(ctx, http.MethodGet, svgTemplateURI, nil) |
| 147 | + if err != nil { |
| 148 | + return nil, err |
| 149 | + } |
| 150 | + |
| 151 | + response, err := http.DefaultClient.Do(httpReq) |
| 152 | + if err != nil { |
| 153 | + return nil, err |
| 154 | + } |
| 155 | + defer response.Body.Close() |
| 156 | + |
| 157 | + if response.StatusCode != http.StatusOK { |
| 158 | + err := errors.New("non ok response code from svg template origin") |
| 159 | + return nil, err |
| 160 | + } |
| 161 | + |
| 162 | + responseData, err := io.ReadAll(response.Body) |
| 163 | + if err != nil { |
| 164 | + return nil, err |
| 165 | + } |
| 166 | + |
| 167 | + template = base64.StdEncoding.EncodeToString([]byte(responseData)) |
140 | 168 | } |
141 | 169 |
|
142 | | - responseData, err := io.ReadAll(response.Body) |
143 | | - if err != nil { |
144 | | - return nil, err |
145 | | - } |
146 | | - |
147 | | - template := base64.StdEncoding.EncodeToString([]byte(responseData)) |
148 | | - |
149 | 170 | reply := &vcclient.SVGTemplateReply{ |
150 | 171 | Template: template, |
151 | 172 | } |
|
0 commit comments